【发布时间】:2014-08-28 14:18:14
【问题描述】:
我首先将图像保存到 Google Cloud Storage,并将 mime 类型设置为“image/png”之类的内容,并以文件结尾的方式随机命名,例如“.png”。这些文件似乎存储正确。我可以在数据存储查看器中看到它们:
但如果我尝试使用com.google.appengine.api.images.ImagesService 再次加载它并调用ImagesService.getServigUrl(),则会引发错误:
java.lang.IllegalArgumentException: NOT_IMAGE: Failed to read image
所以这似乎认为它不是图像。但为什么?它有 mime 类型集,它有文件结尾。代码如下所示:
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);
【问题讨论】:
-
您是从开发服务器还是实时运行代码? stackoverflow.com/questions/12868646/…
-
是的,它是开发服务器。但另一方面,对于 Blobstore,它适用于我的开发服务器。链接的问题没有解决方案..
-
如果您阅读了 cmets,它说他在现场尝试时可以工作“似乎 getServingUrl 仅在 App Engine 测试服务器上失败”尝试部署它。
-
我也遇到了这个问题,并在这篇文章中找到了解决方案:stackoverflow.com/questions/23778367/…
标签: java google-app-engine google-cloud-storage