Get Google Cloud Storage File From Your BlobKey

I wrote a Google App Engine application that uses Blobstore to save software-generated data. For this, I used the file API , which, unfortunately, is deprecated in favor of Google cloud storage. Therefore, I am rewriting my helper class to work with GCS.

I would like to keep the interface as similar as possible, as it was before, also because I save BlobKeys in the data store to support file references (and changing the model of a production application is always painful). When I save something in GCS, I get BlobKey with

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

as prescribed here , and I store it in a data warehouse.

So here's the question: the documentation tells me how to maintain a GCS file using blobstoreService.serve(blobKey, resp); in the servlet's answer, BUT how can I get the contents of a file (like an InputStream, an array of bytes or something else) to use it in my code for further processing? In my current implementation, I am doing this with a FileReadChannel from AppEngineFile showing (both deprecated).

+6
source share
4 answers

Here is the code to open the Google storage object as input. Unfortunately, you have to use the name and the name of the bucket, 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); 
+3
source

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

 BlobstoreInputStream in = new BlobstoreInputStream(blobKey); 
+3
source

You can get the cloudstorage file name only in the download handler (fileInfo.gs_object_name) and save it in your database. After that, it is lost and does not seem to be stored in BlobInfo or other metadata structures.

Google says: Unlike BlobInfo metadata, FileInfo metadata is not stored in the data warehouse. (There is no key either, but you can create it later if necessary, call create_gs_key.) You must save gs_object_name yourself in your load handler, or this data will be lost.

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

0
source

Here is the Blobstore approach (sorry, this is for Python, but I'm sure you will find it very similar for Java):

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

Source: https://habr.com/ru/post/952120/


All Articles