Java – get Google cloud storage files from blobkey

I wrote a Google App Engine application that uses blobstore to store data generated programmatically To this end, I used the files API. Unfortunately, it has been deprecated and supports Google cloud storage So I'm rewriting my helper class to use GCs

I want to keep the interface as similar as before, also because I keep blobkeys in the data store to keep references to files (and it's always painful to change the model of the production application) When I save something to GCs, I retrieve a blobkey

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

According to the rules of here, I insist on in the data storage area

So that's the problem: the document tells me how to use blobstoreservice Serve (blobkey, resp) provides GCS files; In the servlet response, but how do I retrieve the contents of the file (such as InputStream, byte array, or others) to use it for further processing in my code? In my current implementation, I use filereadchannel read from appenginefile (all deprecated)

Solution

The following is the code to open the Google storage object as an input stream Unfortunately, you must use the bucket name and object name instead of 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>