从App Engine中的Google Cloud Storage获取图像

时间:2016-03-01 19:17:39

标签: google-app-engine google-cloud-storage google-cloud-platform

我正在尝试在App Engine后端获取图像,每次尝试获取图像时都会出现以下错误

com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
 {
   "code": 503,
   "errors": [
     {
       "domain": "global",
       "message": "java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.",
       "reason": "backendError"
     }
   ],
   "message": "java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."
 }

现在我的理解是,当从App Engine后端发出请求时,Application Default Credentials已经足够了。

  

Application Default Credentials提供了一种简单的获取方式   用于调用Google API的授权凭据。

     

它们最适合呼叫需要相同的情况   独立于应用程序的身份和授权级别   用户。这是授权调用的推荐方法   Google Cloud API,尤其是在您构建应用程序时   部署到Google App Engine或Google Compute Engine虚拟版   机等。

取自here

这就是我尝试使用Java API

获取图片的方式
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential.getApplicationDefault();

if(credential.createScopedRequired()){
   credential = credential.createScoped(StorageScopes.all());
}

Storage.Builder storageBuilder = new Storage.Builder(httpTransport,new JacksonFactory(),credential);
Storage storage = storageBuilder.build();

Storage.Objects.Get getObject = storage.objects().get("myBucket", name);

ByteArrayOutputStream out = new ByteArrayOutputStream();
            getObject.getMediaHttpDownloader().setDirectDownloadEnabled(false);
getObject.executeMediaAndDownloadTo(out);

byte[] oldImageData = out.toByteArray();
out.close();

ImagesService imagesService = ImagesServiceFactory.getImagesService();

Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
Transform resize = ImagesServiceFactory.makeResize(width, height);

return imagesService.applyTransform(resize, oldImage);

我只是使用了凭据错误,还是我不能使用应用程序默认凭据?

1 个答案:

答案 0 :(得分:0)

如果您想从App Engine访问Google云端存储数据。您应该使用Google Cloud Storage Client Library

Github Project