Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a Google Cloud Storage file from its BlobKey

I wrote a Google App Engine application that makes use of Blobstore to save programmatically-generated data. To do so, I used the Files API, which unfortunately has been deprecated in favor to Google Cloud Storage. So I'm rewriting my helper class to work with GCS.

I'd like to keep the interface as similar as possible as it was before, also because I persist BlobKeys in the Datastore to keep references to the files (and changing the model of a production application is always painful). When i save something to GCS, i retrieve a BlobKey with

BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName());

as prescribed here, and I persist it in the Datastore.

So here's the question: the documentation tells me how to serve a GCS file with blobstoreService.serve(blobKey, resp); in a servlet response, BUT how can I retrieve the file content (as InputStream, byte array or whatever) to use it in my code for further processing? In my current implementation I do that with a FileReadChannel reading from an AppEngineFile (both deprecated).

like image 370
Skice Avatar asked Aug 20 '13 15:08

Skice


People also ask

Where is Google Cloud data stored?

The default bucket location is within the US. If you do not specify a location constraint, then your bucket and data added to it are stored on servers in the US.

Can I upload files to Google Cloud Storage from URL?

Solution OverviewShipyard helps you download a file from any publicly accessible URL and upload it directly to Google Cloud Storage for usage at a later time.


1 Answers

Here is the code to open a Google Storage Object as Input Stream. Unfortunately, you have to use bucket name and object name and not the blob key

GcsFilename gcs_filename = new GcsFilename(bucket_name, object_name);
GcsService service = GcsServiceFactory.createGcsService();
ReadableByteChannel rbc = service.openReadChannel(gcs_filename, 0);
InputStream stream = Channels.newInputStream(rbc);
like image 80
Deviling Master Avatar answered Sep 30 '22 17:09

Deviling Master