oroblam oroblam - 6 months ago 47
Java Question

Get Google Cloud Storage File from ObjectName

I'm migrating my GAE app from the deprecated File API to Google Cloud Storage Client Library.
I used to persist the blobKey, but since there is partial support for it (as specified here) from now on I'll have to persist the object name.
Unfortunately the object name that comes from the GCS looks more or less like this


as you can see, it also contains the bucket name

Here's the issue, every time I need to get the file for further processing (or to serve it in a servlet) I need to create an instance of GcsFileName(bucketName, objectName) which gives me something like


which (of course) doesn't work.

so. my question is:
- how can I generate a GcsFileName form the objectName?


I tried using the objectName as BlobKey. But it just doesn't work :(

InputStream is = new BlobstoreInputStream(blobstoreService.createGsBlobKey("/gs/bucketName/akahsdjahslagfasgfjkasd"));

I got the usual answer

BlobstoreInputStream received an invalid blob key

How do I get the file using the ObjectName???


If you have persisted and retrieved e.g the string String objname worth e.g "/gs/bucketname/819892hjd81dh19gf872g8211", you could split it on "/" (String[] pieces = objname.split("/")) and use the pieces appropriately in the call to GcsFileName.