设置GOOGLE_APPLICATION_CREDENTIALS仍然无法访问Google存储空间

时间:2018-03-09 04:01:23

标签: java google-cloud-storage

This answer显示了如何使用Java中的服务帐户访问Google云端存储 使用硬编码服务帐户凭据,我可以访问GCS。 但是,使用GOOGLE_APPLICATION_CREDENTIALS或正在运行gcloud auth application-default login,我无法访问GCS并获得GoogleJsonResponseException: 401 Unauthorized。 我的操作系统是Win7_64x。

Exception in thread "main" com.google.cloud.storage.StorageException: Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar.
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:191)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:342)
    at com.google.cloud.storage.StorageImpl$5.call(StorageImpl.java:198)
    at com.google.cloud.storage.StorageImpl$5.call(StorageImpl.java:195)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
    at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:195)
    at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:209)
    at com.example.storage.QuickstartSample.main(QuickstartSample.java:47)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar.",
    "reason" : "required"
  } ],
  "message" : "Anonymous caller does not have storage.objects.get access to nmjcloud_jar_test/addons/simple-bean-1.0.jar."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:340)
    ... 8 more

我如何查看我错在哪里?

1 个答案:

答案 0 :(得分:1)

亲爱的,亲爱的。我犯了一个错误。

我在变量值的文本框中引用了变量值 不需要使用引号来包含该字符串。

不正确:“/ path / to / jsonfile.json”
  正确:/ path / to / jsonfile.json