Uploading files directly to Google Cloud Storage f

2019-02-25 05:16发布

I am looking at switching from Blobstore to Google Cloud Storage for things like image uploads in a project (since Google calls Blobstore "superseded").

In Blobstore, the multipart form would be submitted (uploaded) directly to the Blobstore, which would then rewrite the request and redirect to the GAE app for handling the BlobKey(s). This meant very little load on the GAE app.

Is there a way to implement a similar workflow with Google Cloud Storage (GCS)?

The example given at Reading and Writing to Google Cloud Storage involves GAE code handling the upload, reading the data and saving it in GCS.

Similarly for serving the (for example) uploaded image file: seems like a handler has to be implemented in one's GAE code to read from GCS and return the image data.

Isn't there a way to simply generate a URL for a resource in GCS (AFAIK AWS S3 supports this) and let it be served from there?

2条回答
疯言疯语
2楼-- · 2019-02-25 05:23

Google Cloud Storage does support generating URL for upload or reads. For more information see: https://cloud.google.com/storage/docs/access-control/create-signed-urls-program

The gcloud-java-storage library can help with creating such signed URL.

查看更多
三岁会撩人
3楼-- · 2019-02-25 05:38

You can keep using the blobstore API but with actual storage in GCS, meaning same upload behaviour and probably minimal changes to your app.

From Using the Blobstore API with Google Cloud Storage (Python):

You can use the Blobstore API to store blobs in Cloud Storage instead of storing them in Blobstore. You need to set up a bucket as described in the Google Cloud Storage documentation and specify the bucket and filename in the blobstore.blobstore.create_upload_url gs_bucket_name parameter. In your upload handler, you need to process the returned FileInfo metadata and explicitly store the Google Cloud Storage filename needed to retrieve the blob later.

A complete example app is presented there as well.

Update: Oops, the above answer applies to the Python environment, the Java one follows:

From Using the Blobstore API with Google Cloud Storage (Java):

You can use the Blobstore API to store blobs in Cloud Storage instead of storing them in Blobstore. You need to set up a bucket as described in the Google Cloud Storage documentation and specify the bucket and filename in the BlobstoreService createUploadUrl, specify the bucket name in the UploadOptions parameter. In your upload handler, you need to process the returned FileInfo metadata and explicitly store the Google Cloud Storage filename needed to retrieve the blob later.

There is no complete sample app for Java, tho.

查看更多
登录 后发表回答