1

I first save an image to Google Cloud Storage also setting the mime type to something like "image/png" and name it randomly with the file ending e.g. ".png". The files seem to be stored correctly. I can see them in the datastore viewer:

enter image description here

But if I try to load it again with the com.google.appengine.api.images.ImagesService and call ImagesService.getServigUrl(), it throws the error:

java.lang.IllegalArgumentException: NOT_IMAGE: Failed to read image

So this seems to think it is not an image. But why? It has the mime typ set, it has the file ending. The code looks like this:

ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(crop, oldImage);

final GcsService gcsService =
        GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());

AppIdentityService appservice =  com.google.appengine.api.appidentity.AppIdentityServiceFactory.getAppIdentityService();

GcsFilename fileName = new GcsFilename(appservice.getDefaultGcsBucketName(), Long.toString(random.nextLong(), 36)+"."+newImage.getFormat().toString().toLowerCase());

GcsFileOptions gcsOptions = new GcsFileOptions.Builder()
  .mimeType("image/"+newImage.getFormat().toString().toLowerCase())
  .acl("public-read").build();

GcsOutputChannel outputChannel =
        gcsService.createOrReplace(fileName, gcsOptions);

ObjectOutputStream oout =
    new ObjectOutputStream(Channels.newOutputStream(outputChannel));
oout.writeObject(newImage.getImageData());
oout.close();


ServingUrlOptions suo = ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/"+fileName.getBucketName()+"/"+fileName.getObjectName()).secureUrl(true);
String s = imagesService.getServingUrl(suo);
System.out.println(s);
jan
  • 3,923
  • 9
  • 38
  • 78
  • Are you running your code from the Dev server or Live? http://stackoverflow.com/questions/12868646/google-app-engine-imagesservice-getservingurl-fails-to-read-image – Ryan Aug 28 '14 at 14:34
  • Yes, it is the dev server. But for the Blobstore on the on the other hand it works on the dev server for me. The linked question has no solution.. – jan Aug 28 '14 at 14:59
  • If you read the comments it says it worked when he tried it live "It seems getServingUrl fails only on the App Engine test server " Try deploying it. – Ryan Aug 28 '14 at 15:05
  • I also faced this issue and found the solution on this post: http://stackoverflow.com/questions/23778367/photo-dont-uploaded-correctly-gcs-app-engine/34069463#34069463 – Kevin Glenny Dec 03 '15 at 15:21

1 Answers1

0

Might be it is not a image mime type you have uploaded,check your mime type of the uploaded file.