[英]Python App Engine serving files with Google Cloud Storage
I currently use the following code for allowing my users to upload files; 我目前使用以下代码允许我的用户上传文件;
uploadurl = blobstore.create_upload_url('/process?session=' + session, gs_bucket_name='mybucketname')
and I can serve images like this; 我可以提供这样的图像;
imgurl = get_serving_url(blob_key, size=1600, crop=False, secure_url=True)
After content is uploaded using the method in the first code snipped, the blob key contains encoded_gs_file:
and that's how it knows to serve it from Google Cloud Service and not the blobstore as standard. 使用第一个代码片段中的方法上传内容后,blob密钥包含
encoded_gs_file:
这就是它知道如何从Google Cloud Service而不是blobstore提供服务的标准方式。
However, I'm unsure how I'd serve any other kind of file (for example .pdf, or .rtf). 但是,我不确定如何提供其他任何类型的文件(例如.pdf或.rtf)。 I do not want the content to be displayed in the browser, but rather sent to the client as a download (so they get the save file dialog and choose a location on their computer to save it).
我不希望将内容显示在浏览器中,而是将其作为下载内容发送给客户端(因此,他们会看到“保存文件”对话框,并在计算机上选择一个位置进行保存)。
How would I go about doing this? 我将如何去做呢? Thanks.
谢谢。
Using a google serving_url works only for images. 使用google serve_url仅适用于图像。
To serve a pdf from the blobstore you can use: 要从Blobstore提供pdf,可以使用:
class DynServe(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
(blob_key, extension) = resource.rpartition('.')[::2]
blob_info = blobstore.BlobInfo.get(blob_key)
if not blob_info:
logging.error('Blob NOT FOUND %s' % resource)
self.abort(404)
self.response.headers[b'Content-Type'] = mimetypes.guess_type(blob_info.filename)
self.send_blob(blob_key, save_as=blob_info.filename)
The webapp2 route for this handler looks like: 此处理程序的webapp2路由如下所示:
webapp2.Route(r'/dynserve/<resource:(.*)>', handler=DynServe)
To serve: 服务:
<a href="/dynserve/<blob_key>.pdf">PDF download</a>
I'm going to answer my own question based on the answer from @voscausa 我要根据@voscausa的回答来回答自己的问题
This is what my handler looks like (inside a file named view.py
); 这就是我的处理程序的样子(在名为
view.py
的文件中);
class DynServe(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
blob_key = resource
if not blobstore.get(blob_key):
logging.warning('Blob NOT FOUND %s' % resource)
self.abort(404)
return
else:
blob_info = blobstore.BlobInfo.get(blob_key)
self.send_blob(blob_key, save_as=blob_info.filename)
We need this in app.yaml
; 我们在
app.yaml
需要它;
- url: /download/.*
script: view.app
secure: always
secure: always
is optional, but I always use it while handling user data. secure: always
是可选的,但是我在处理用户数据时总是使用它。
Put this at the bottom of view.py
; 把它放在
view.py
的底部;
app = webapp.WSGIApplication([('/download/([^/]+)?', DynServe),
], debug=False)
Now visit /download/BLOB_KEY_HERE. 现在访问/ download / BLOB_KEY_HERE。 (you can check the datastore for your blob key)
(您可以检查数据存储区中的Blob键)
That's a fully working example which works with both the standard blob store AND Google Cloud Service. 这是一个完全正常的示例,可与标准Blob存储和Google Cloud Service一起使用。
NOTE: All blob keys which are part of the GCS will start with encoded_gs_file:
and the ones which don't are in the standard blobstore; 注意:属于GCS的所有Blob密钥都将以
encoded_gs_file:
开头encoded_gs_file:
那些不在标准Blobstore中; app engine automatically uses this to determine where to locate the file app引擎会自动使用它来确定文件的位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.