我們目前使用blobstore.create_upload_url來創建前端使用的上傳地址,請參閱Uploading a blob。 但是,隨着谷歌推動Google雲存儲(GCS),我想使用GCS而不是blobstore。我們目前使用blobstore.create_upload_url,但在GCS文檔中找不到任何相同的內容。我錯過了什麼嗎?有沒有更好的方法從前端上傳文件到GCS?什麼是blobstore「Create_upload_url」的GCS等效物?
感謝 羅布
我們目前使用blobstore.create_upload_url來創建前端使用的上傳地址,請參閱Uploading a blob。 但是,隨着谷歌推動Google雲存儲(GCS),我想使用GCS而不是blobstore。我們目前使用blobstore.create_upload_url,但在GCS文檔中找不到任何相同的內容。我錯過了什麼嗎?有沒有更好的方法從前端上傳文件到GCS?什麼是blobstore「Create_upload_url」的GCS等效物?
感謝 羅布
如果您將提供gs_bucket_name
到blobstore.create_upload_url
文件將被存儲在GCS而不是Blob存儲,這是官方的文檔中描述: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"))
也請注意,GCS也有這個隱藏在api的xml部分。請參閱https://cloud.google.com/storage/docs/access-control/create-signed-urls-program和https://cloud.google.com/storage/docs/xml-api/post-object –
看看[GCS文件上傳](http:// romanno wicki.readthedocs.io/en/latest/gae/file-upload.html#file-upload)對於GCS,您仍然可以使用blobstore.create_upload_url,在此處的文檔中對其進行了描述:[在Google雲存儲中使用Blobstore API](https ://cloud.google.com/appengine/docs/python/blobstore/) – manRo
謝謝@manRo所以指定一個bucketname是所有需要發生的blob去gcs而不是blobstore?也許把它放在答案中,我可以接受它。 –
是的,這是正確的,如果你將提供桶名稱文件將被上傳到GCS – manRo