简体   繁体   English

如何使用google-cloud客户端将大于32MB的文件上传到GCS?

[英]How can I upload a file larger than 32MB to GCS using the google-cloud client?

I recently started using google-cloud client , and I made this little example (I have omitted/changed things for clarity's sake): 我最近开始使用google-cloud client ,并做了一个小例子(为了清楚起见,我省略/更改了一些内容):

<form id="testform" method="post" enctype="multipart/form-data">
  <p>
    <div>
      <label for="fotos">Some file</label>
    </div>
    <div>
      <input type="file" name="testfile">
    </div>
  </p>
  <input type="submit" value="Submit">
</form>
from google.cloud import storage

class MyHandler(BaseHandler):
    def get(self):
        self.render("test.html")

    def post(self):
        file = self.request.POST["testfile"]
        filename = file.filename # + TimeStamp

        bucket = storage.Client().get_bucket(self.get_bucket_name())
        blob = bucket.blob(filename)

        blob.upload_from_string(data=file.file.read(),
                                content_type=file.type)

        self.write(blob.public_url)

This code works just fine with anything under 32MB, but it cannot handle larger files because they go through the app instead of directly to GCS. 此代码适用于32MB以下的任何内容,但无法处理较大的文件,因为它们会通过应用程序而不是直接传递到GCS。 I have seen solutions using the Blobstore API, but those are old and the Blobstore isn't even used to store the data anymore (though the Blobstore API can still be used). 我已经看到了使用Blobstore API的解决方案 ,但是这些解决方案比较老,并且Blobstore甚至不再用于存储数据(尽管Blobstore API仍然可以使用)。

So I was wondering, is there a way to fix this using the google-cloud client? 所以我想知道,有没有一种方法可以使用Google-cloud客户端解决此问题? I have not seen a method to create an upload url in the docs . 我还没有在docs中看到创建上传网址的方法。

Client should upload directly to GCS. 客户应直接上传到GCS。 A single connection from a GAE can't transfer more than 32 MB. 来自GAE的单个连接最多可以传输32 MB。

This shouldn't be a problem to you because you are already using a HTML form. 这对您来说应该不是问题,因为您已经在使用HTML表单。 https://cloud.google.com/storage/docs/xml-api/post-object https://cloud.google.com/storage/docs/xml-api/post-object

You may have to make the bucket publicly accessible depending on the access you'll need for your app. 您可能必须使存储桶可公开访问,具体取决于您对应用程序的访问权限。 Else you'll have to specify GoogleAccessId in the PUT request generated on your backend and sent to frontend beforehand. 否则,您必须在后端生成并事先发送到前端的PUT请求中指定GoogleAccessId

There is a small form upload example at the end of the page linked above. 上面链接的页面末尾有一个小型表单上传示例。

Finally, I personally recommend to use AJAX to upload your file using the REST API https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload This will help you handle errors, redirects and success easier. 最后,我个人建议使用REST API https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload使用AJAX上传文件,这将帮助您更轻松地处理错误,重定向和成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Google App Engine 上传大于 32mb 的文件 - Django Google App Engine Upload files greater than 32mb AppEngine是否可以“拉”出超过32MB的数据? - Can AppEngine *pull* in data over 32MB? pip安装google-cloud后找不到模块google_auth_httplib2。如何解决? - Module google_auth_httplib2 not found after pip installing google-cloud How can I fix it? 我无法使用python脚本将文件上传到谷歌云? - I can't upload a file to google cloud using python script? 使用pip安装google-cloud失败 - installing google-cloud using pip fails 如何使用AJAX将图像上传到Google云存储? - How can I upload an image using AJAX to google cloud storage? Google 云存储:将字符串上传到 GCS 时 CRC32C 和 MD5 不匹配 - Google cloud storage: Mismatch in CRC32C & MD5 while upload string to GCS 使用 Cloud function 从 url 下载大文件 (800MB) 到 GCS 桶中 - Downloading big file(800MB) from url into GCS bucket using Cloud function 如何使用适用于 Cloud Functions 的 Google Python 客户端获取 Google Cloud Functions 的列表? - How can I get a list of Google Cloud Functions using Google Python Client for Cloud Functions? 如何使用 Python 从 AWS S3 发送/复制/上传文件到 Google GCS - How to send/copy/upload file from AWS S3 to Google GCS using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM