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