简体   繁体   English

Python App Engine通过Google Cloud Storage提供文件

[英]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.

相关问题 使用Google App Engine将文件上传到Google云端存储(Python) - Upload Files To Google Cloud Storage With Google App Engine (Python) 从python后端到Google Cloud Storage中文件的公共URL(Google App Engine) - Public URL to files in Google Cloud Storage from python backend (Google App Engine) 在Google App Engine(python)中从Google Cloud Storage读取文件时发生内存泄漏 - Memory leak in reading files from Google Cloud Storage at Google App Engine (python) 从 Google App Engine 修改 Google Cloud Storage 中的文件 - Modifying files in Google Cloud Storage from Google App Engine Google App Engine:如何将大型文件写入Google云端存储 - Google App Engine: How to write large files to Google Cloud Storage Google App Engine + Google Cloud Storage + Sqlite3 + Django / Python - Google App Engine + Google Cloud Storage + Sqlite3 + Django/Python Google App Engine,云存储无法读取python中的上传文件 - Google App Engine, Cloud Storage can't read uploaded files in python 如何在App Engine开发服务器上获取Google Cloud Storage文件对象的服务URL? - How do I get the serving URL for Google Cloud Storage file objects on App Engine dev server? 无法在Google App Engine和Python上显示来自云存储的图像 - Trouble displaying image from cloud storage on Google App Engine, Python 如何在 App Engine Python 上创建 Google Cloud Storage 签名 URL - How to Create Google Cloud Storage Signed Urls on App Engine Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM