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