Rob Curtis Rob Curtis - 2 months ago 14
Python Question

What is the GCS equivalent of the blobstore "Create_upload_url"?

We currently use blobstore.create_upload_url to create upload urls to be used on the frontend see Uploading a blob.
However, with the push toward Google Cloud Storage (GCS) by Google, I'd like to use GCS instead of the blobstore. We use currently blobstore.create_upload_url but I can't find anything equivalent in the GCS documentation. Am I missing something? Is there a better way to upload files to GCS from the frontend?

Thanks
Rob

Answer

If you will provide gs_bucket_name to blobstore.create_upload_url file will be stored in GCS instead of blobstore, this is described in official documentation: Using the Blobstore API with Google Cloud Storage

blobstore.create_upload_url(
                success_path=webapp2.uri_for('upload'),
                gs_bucket_name="mybucket/dest/location")

You can take a look at simple upload handler implementation made in 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"))