2016-09-17 123 views
2

我们目前使用blobstore.create_upload_url来创建前端使用的上传地址,请参阅Uploading a blob。 但是,随着谷歌推动Google云存储(GCS),我想使用GCS而不是blobstore。我们目前使用blobstore.create_upload_url,但在GCS文档中找不到任何相同的内容。我错过了什么吗?有没有更好的方法从前端上传文件到GCS?什么是blobstore“Create_upload_url”的GCS等效物?

感谢 罗布

+0

看看[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

+0

谢谢@manRo所以指定一个bucketname是所有需要发生的blob去gcs而不是blobstore?也许把它放在答案中,我可以接受它。 –

+1

是的,这是正确的,如果你将提供桶名称文件将被上传到GCS – manRo

回答

3

如果您将提供gs_bucket_nameblobstore.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")) 
+0

也请注意,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 –

相关问题