英文:
GAE: How to upload files to be accessed by the application?
问题
**更新:我找到了问题。**即使模板文件在子目录中,我的HTML文件的静态规则仍然匹配了这些模板文件。在将规则更改为不再匹配它们之后,应用程序可以访问这些文件。
我正在创建一个Google AppEngine应用程序。我正在使用Go语言,但我不确定这是否相关。我想使用一个带有单独模板文件的模板。因此,应用程序必须读取模板文件。在本地,这个工作正常,但是在将应用程序上传到GAE之后,当调用template.ParseFiles()时,我得到了“没有这样的文件或目录”的错误。
所以我在思考,我必须以某种方式指示模板文件是应用程序文件,以便它们以正确的方式上传。我想也许我的模板文件由于在app.yaml中的以下内容而被上传为静态文件:
- url: /(.*\.html)
static_files:
upload: .*\.html
所以我将它们移动到了它们自己的目录中。但是这没有任何区别。我尝试在app.yaml中为我的脚本规则添加一个上传指令,像这样:
- url: /.*
script: _go_app
upload: templates/.*
但是似乎不允许这样做。那么有人可以告诉我如何从我的GAE应用程序中访问模板文件(或任何数据文件)吗?
谢谢!
英文:
Update: I found the problem. My static rule for HTML files still matched the templates even though they were in a subdirectory. After changing the rule to not match them anymore, the files could be accessed by the application.
I'm creating a Google AppEngine application. I'm using the Go language but I'm not sure that's relevant. I want to use a template with a separate template file. So the application must read the template file. Locally this works but after uploading the app to GAE, I get "no such file or directory" when calling template.ParseFiles().
So I'm thinking that somehow I must indicate that the template files are application files so that they get uploaded in the right way. I thought maybe my template files were uploaded as static files because of this in my app.yaml
- url: /(.*\.html)
static_files:
upload: .*\.html
So I moved them to their own directory. But that didn't make a difference. I tried adding an upload directive for my script rule in app.yaml like this:
- url: /.*
script: _go_app
upload: templates/.*
But that didn't seem to be allowed. So can anyone tell me how do I access the template files (or any data files) from my GAE application?
Thanks!
答案1
得分: 1
你的项目目录中的所有文件都会自动上传。
如果你的应用程序需要访问它们(因为你正在使用模板),你不能将它们标记为静态文件。这就是为什么第一个解决方案失败的原因。
第二个尝试也不起作用,因为上传不是url描述的成员。
在你的情况下,只需部署你的应用程序,文件将自动上传并可供应用程序使用(你可能需要获取项目的基本路径来构建模板的完整路径)。
英文:
All files in your project directory are automatically uploaded.
If your app needs to access them (this is your case as you are working with templates) you can't mark them as static. This is why the first solution fails.
The second attempt, does not work either as upload is not a member of an url description.
In your case, just deploy your app and the files will be automatically uploaded and available to your app (you may need to get the basepath of your project to construct the full path to the template).
答案2
得分: 1
这些文件会自动上传,假设它们不在static_dir
或static_files
目录中。请参阅App Engine文档中的Skipping Files部分。App Engine将这些目录和文件视为静态资源,并且静态资源会单独提供服务。
通常情况下,应用程序代码无法访问静态资源。如果您必须将模板放在与其他静态资源相同的目录中,您可以使用application_readable
设置使这些资源对您的应用程序可访问。
但正如您所指出的,您可以将模板放在应用程序中未映射为静态资源路径的位置;这可能是正确的做法。
英文:
The files are automatically uploaded, assuming they're not in a static_dir
or static_files
directory. See the Skipping Files section in the App Engine documentation. App Engine treats such directories and files as static resources, and static resources are served separately from the application.
Static resources are not normally accessible from application code. If you must put your templates in the same directory as your other static resources, you can use the application_readable
setting to make such resources accessible to your app as well.
But as you note, you can put your templates in a place within your app that isn't mapped to a static resource path; that's probably the right thing to do.
答案3
得分: 0
查看app.yaml的不同选项,请参阅1。您可以查看static_dir和static_files选项。我分享一个可以下载静态文件的示例。这些文件位于static文件夹和test文件夹中。我分享我的app.yaml:
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.app
- url: /test
static_dir: test
application_readable: true
- url: /(.*\.(gif|png|jpg))$
static_files: static/
upload: static/.*\.(gif|png|jpg)$
application_readable: true
以及main.py:
…
import webapp2
...
class Download(webapp2.RequestHandler):
def get(self):
imageName = self.request.get('img-name')
self.response.headers['Content-Disposition'] = str('attachment; filename=' + imageName)
f = None
try:
f = open('static/' + imageName, 'r')
self.response.out.write(f.read())
except:
self.response.out.write('Jon Wayne')
class Test(webapp2.RequestHandler):
def get(self):
imageName = self.request.get('tst-name')
self.response.headers['Content-Disposition'] = str('attachment; filename=' + imageName)
f = None
try:
f = open('test/' + imageName, 'r')
self.response.out.write(f.read())
except:
self.response.out.write('Jon Test')
app = webapp2.WSGIApplication([
('/download', Download),
('/tst', Test)
], debug=True)
我有两个图片:fib2.png和fib1.jpeg
然后在Test文件夹中有两个文件fib2.tst和fib1.tst。
Web是域名。
https://web.com/download?img-name=fib2.png
https://web.com/download?img-name=fib1.jpeg
https://web.com/tst?tst-name=fib2.tst
https://web.com/tst?tst-name=fib1.tst
希望对您有所帮助!
1 https://cloud.google.com/appengine/docs/standard/go/config/appref#handlers_element
英文:
Check the different options for the app.yaml at 1. You can check the static_dir and static_files options. I’m sharing a sample that is able to download static files. These files are in the static folder adn test folder. I share my app.yaml:
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.app
- url: /test
static_dir: test
application_readable: true
- url: /(.*\.(gif|png|jpg))$
static_files: static/
upload: static/.*\.(gif|png|jpg)$
application_readable: true
And the main.py:
…
import webapp2
...
class Download(webapp2.RequestHandler):
def get(self):
imageName = self.request.get('img-name')
self.response.headers['Content-Disposition'] = str('attachment; filename=' + imageName)
f = None
try:
f = open('static/' + imageName, 'r')
self.response.out.write(f.read())
except:
self.response.out.write('Jon Wayne')
class Test(webapp2.RequestHandler):
def get(self):
imageName = self.request.get('tst-name')
self.response.headers['Content-Disposition'] = str('attachment; filename=' + imageName)
f = None
try:
f = open('test/' + imageName, 'r')
self.response.out.write(f.read())
except:
self.response.out.write('Jon Test')
app = webapp2.WSGIApplication([
('/download', Download),
('/tst', Test)
], debug=True)
I have two images: fib2.png and fib1.jpeg
Then in the Test folder I have two files fib2.tst and fib1.tst.
Web is the domain name.
https://web.com/download?img-name=fib2.png
https://web.com/download?img-name=fib1.jpeg
https://web.com/tst?tst-name=fib2.tst
https://web.com/tst?tst-name=fib1.tst
Hope this helps!
1 https://cloud.google.com/appengine/docs/standard/go/config/appref#handlers_element
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论