Get a Google Cloud Storage file from its BlobKey

心不动则不痛 提交于 2019-12-22 05:24:15

问题


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).


回答1:


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);



回答2:


Given a blobKey, use the BlobstoreInputStream class to read the value from Blobstore, as described in the documentation:

BlobstoreInputStream in = new BlobstoreInputStream(blobKey); 



回答3:


You can get the cloudstorage filename only in the upload handler (fileInfo.gs_object_name) and store it in your database. After that it is lost and it seems not to be preserved in BlobInfo or other metadata structures.

Google says: Unlike BlobInfo metadata FileInfo metadata is not persisted to datastore. (There is no blob key either, but you can create one later if needed by calling create_gs_key.) You must save the gs_object_name yourself in your upload handler or this data will be lost.

Sorry, this is a python link, but it should be easy to find something similar in java. https://developers.google.com/appengine/docs/python/blobstore/fileinfoclass




回答4:


Here is the Blobstore approach (sorry, this is for Python, but I am sure you find it quite similar for Java):

blob_reader = blobstore.BlobReader(blob_key)
if blob_reader:
  file_content = blob_reader.read()


来源:https://stackoverflow.com/questions/18339168/get-a-google-cloud-storage-file-from-its-blobkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!