4

I have little js programm that uploads an image to my gcs bucket. If thi happens, my gcs sends a notification to a servlet at google app engine.

So far, so good. Now my servlet should read from gcs and download the file, which was uploaded a moment ago. Then i want to parse it into an image object and resize the image. The new image should be uploaded to another gcs bucket.

To communicate with gcs i use the 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 to save it in a byte array but the Error is always the same

Uncaught exception from servlet java.io.StreamCorruptedException: invalid stream header: FFD8FFE0

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

Laslo89
  • 105
  • 7
  • Using App Engine's ImageService URLs to serve resized images may be an easier and cheaper option than resizing yourself and saving different versions. – Price Nov 13 '15 at 11:09
  • thanks for your quick answer, do you have link where this is explained in detail? – Laslo89 Nov 13 '15 at 11:17
  • especially when it comes to saving data into blobstore from a browser client with javascript – Laslo89 Nov 13 '15 at 11:25
  • See the getServingURL() method on this page: https://cloud.google.com/appengine/docs/java/images/#Java_Transforming_images_from_the_Blobstore. You can wrap a call to this method in a cloud endpoint API method and call it from a javascript client. – Price Nov 13 '15 at 11:41

1 Answers1

3

Use the ImageService like Price suggested:

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

Then, you can use these parameters to transform the served image on the fly.

Community
  • 1
  • 1
Fábio Uechi
  • 807
  • 7
  • 25