【问题标题】:Get image from Google Cloud Storage in App Engine在 App Engine 中从 Google Cloud Storage 获取图像
【发布时间】:2016-03-01 19:17:39
【问题描述】:

我正在尝试在我的 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 就足够了。

应用程序默认凭据提供了一种简单的方法来获取 用于调用 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);

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

【问题讨论】:

  • 据我了解,如果您使用内部链接 gs:// 与外部链接,它会起作用

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


【解决方案1】:

如果您想从 App Engine 访问您的 Google Cloud Storage 数据。你应该使用Google Cloud Storage Client Library

Github Project

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 2018-07-22
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2014-01-18
    相关资源
    最近更新 更多