【问题标题】:Choosing Google Cloud Storage object name when uploading a file using Python blobstore API使用 Python blobstore API 上传文件时选择 Google Cloud Storage 对象名称
【发布时间】:2015-06-18 18:57:48
【问题描述】:

我正在我的 Google App Engine python 应用程序中从 blobstore 迁移到 Google Cloud Storage。我想在上传到 GCS 时使用 blobstore API。 这完全没问题,根据GAE python documentation regarding blobstore

流程就像存储到 blobstore 一样。客户端请求上传 URL,blobstore.create_upload_url(...) 创建它,客户端通过多部分 POST 上传文件,然后服务器重定向到我的 GAE 应用程序中的上传处理程序。

问题在于,虽然我必须选择 GCS 存储桶(它是 create_upload_url 调用的参数之一),但我不知道如何选择文件名。我想把它分解成文件夹。 灵感来自于

Google App Engine Blobstore to Google Cloud Storage Migration Tool 文件名被破坏以便管理 GCS“文件夹”的浏览。

我只知道一种命名 GCS 文件的方法 - 放弃 blobstore API。 相反,客户端会将文件上传到 GAE 处理程序,该处理程序将使用 lib.cloudstorage 将文件数据写入 GCS - 就像引用的迁移工具一样。

我会失去 GCS 的好处,比如在上传过程中重试错误。

问题:有没有办法将文件直接上传到 GCS,同时影响生成的 GCS 对象的命名方式。

【问题讨论】:

  • 或者在 blobstore gcs 上传后使用 gcs 复制、删除重命名对象
  • 或使用 blobstore 上传并将其移动(写入)到 gcs

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


【解决方案1】:

存储库:https://github.com/voscausa/appengine-gcs-upload

第一个选项的示例代码。

  • 您可以在其中命名存储桶对象
  • 并使用签名的 url 访问存储桶

文档:https://cloud.google.com/storage/docs/reference-methods?hl=bg#postobject

上传模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>'gcs_upload'</title>
</head>
<body>
    <form action="http://storage.googleapis.com/{{ form_bucket }}"
          method="post" enctype="multipart/form-data">
        <input type="text" name="key" value="">
        <input type="hidden" name="GoogleAccessId" value="{{ form_access_id }}">
        <input type="hidden" name="acl" value="bucket-owner-read">
        <input type="hidden" name="success_action_redirect" value="{{ form_succes_redirect }}">
        <input type="hidden" name="policy" value="{{ form_policy }}">
        <input type="hidden" name="signature" value="{{ form_signature }}">
        <input type="file" name="file">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

上传表单处理程序:

class GcsUpload(BaseHandler):

    def get(self):

        default_bucket = app_identity.get_default_gcs_bucket_name()
        google_access_id = app_identity.get_service_account_name()
        succes_redirect = 'http://www.example.com/success'
        policy_string = """
        {"expiration": "2015-06-22T18:11:11Z",
                  "conditions": [
                      ["starts-with", "$key", ""],
                      {"acl": "bucket-owner-read"},
                      {"success_action_redirect": "%s"},
                  ]}""" % succes_redirect

        policy = base64.b64encode(policy_string)
        _, signature_bytes = app_identity.sign_blob(policy)
        signature = base64.b64encode(signature_bytes)

        self.render_template('gcs_upload.html', form_bucket=default_bucket, form_succes_redirect=succes_redirect,
                             form_access_id=google_access_id, form_signature=signature, form_policy=policy)

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 2014-10-25
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 2017-04-02
    • 2019-11-07
    相关资源
    最近更新 更多