我们目前使用blobstore.create_upload_url创建要在前端使用的上传网址,请参阅this video。 但是,随着谷歌推出谷歌云存储(GCS),我想使用GCS而不是blobstore。我们目前使用的是blobstore.create_upload_url,但我在GCS文档中找不到任何相同的内容。我错过了什么吗?有没有更好的方法从前端上传文件到GCS?
由于 罗布
答案 0 :(得分:3)
如果您将提供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"))