How to get image from google cloud storage in java?

I have a small js program that uploads an image to my gcs bucket. If this happens, my gcs will send a notification to the servlet in the google engine.

So far so good. Now my servlet should read from gcs and download the file that was downloaded minutes ago. Then I want to parse it into an image object and resize the image. The new image should be loaded into another gcs bucket.

To communicate with gcs, I use gcs client lybrary.

GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
GcsFilename filename = new GcsFilename("bucket_name", "image_name");
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(filename, 0, 1024 * 1024);

try ( ObjectInputStream inputStream = new ObjectInputStream(Channels.newInputStream(readChannel))){
   inputStream.readObject();
   log(inputStream.getClass().getName());
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I also tried storing it in a byte array, but the error is always the same

Do not throw exception from servlet java.io.StreamCorruptedException: invalid stream header: FFD8FFE0

what am I doing wrong? Did I forget something? Thank you for your help.

+4
1

ImageService, :

GcsFilename gcsFilename = new GcsFilename(bucketDefault, objectName);
String filename = String.format("/gs/%s/%s", gcsFilename.getBucketName(), gcsFilename.getObjectName());
String servingUrl = imageService.getServingUrl(ServingUrlOptions.Builder.withGoogleStorageFileName(filename).secureUrl(true));

.

+3

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


All Articles