【问题标题】:What is the GCS equivalent of the blobstore "Create_upload_url"?Blobstore“Create_upload_url”的 GCS 等效项是什么?
【发布时间】:2016-09-17 10:07:52
【问题描述】:

我们目前使用 blobstore.create_upload_url 创建要在前端使用的上传 url,请参阅 Uploading a blob。 但是,随着 Google 对 Google Cloud Storage (GCS) 的推动,我想使用 GCS 而不是 blobstore。我们目前使用 blobstore.create_upload_url 但我在 GCS 文档中找不到任何等效的东西。我错过了什么吗?有没有更好的方法将文件从前端上传到 GCS?

谢谢 抢

【问题讨论】:

  • 看看GCS File Upload 您仍然可以将 blobstore.create_upload_url 用于 GCS,它在此处的文档中有所描述:Using the Blobstore API with Google Cloud Storage
  • 谢谢@manRo,所以指定一个存储桶名称就可以让 blob 转到 gcs 而不是 blobstore?也许把这个放在答案中,我可以接受。
  • 是的,如果您提供存储桶名称文件将被上传到 GCS,那是正确的

标签: python google-app-engine google-cloud-storage


【解决方案1】:

如果您将gs_bucket_name 提供给blobstore.create_upload_url 文件将存储在GCS 中而不是blobstore 中,这在官方文档中有描述:Using the Blobstore API with Google Cloud Storage

blobstore.create_upload_url(
                success_path=webapp2.uri_for('upload'),
                gs_bucket_name="mybucket/dest/location")

你可以看看 webapp2 中的简单上传处理程序实现

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import webapp2
import cloudstorage as gcs


class Upload(blobstore_handlers.BlobstoreUploadHandler):
    """Upload handler
    To upload new file you need to follow those steps:

    1. send GET request to /upload to retrieve upload session URL
    2. send POST request to URL retrieved in step 1
    """
    def post(self):
        """Copy uploaded files to provided bucket destination"""
        fileinfo = self.get_file_infos()[0]
        uploadpath = fileinfo.gs_object_name[3:]
        stat = gcs.stat(uploadpath)

        # remove auto generated filename from upload path 
        destpath = "/".join(stat.filename.split("/")[:-1])

        # copy file to desired location with proper filename 
        gcs.copy2(uploadpath, destpath)
        # remove file from uploadpath
        gcs.delete(uploadpath)

    def get(self):
        """Returns URL to open upload session"""

        self.response.write(blobstore.create_upload_url(
            success_path=uri_for('upload'),
            gs_bucket_name="mybucket/subdir/subdir2/filename.ext"))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 2018-11-12
  • 2012-10-16
  • 2010-10-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多