GoogleCloudPlatform/storage/cloud-client sample可以在GCE VM上运行。
但是在本地计算机(Win7_64x)上运行时,出现StorageException(401 Unauthorized)。
我通过this doc设置了身份验证。
该程序可以从我的本地计算机访问Google存储吗?
我还应该做些什么来实现这一目标?还是没有办法在GCP之外访问Google存储?
Exception in thread "main" com.google.cloud.storage.StorageException: 401 Unauthorized
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:191)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:221)
at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:112)
at com.google.cloud.storage.StorageImpl$2.call(StorageImpl.java:109)
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.create(StorageImpl.java:108)
at com.example.storage.QuickstartSample.main(QuickstartSample.java:35)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
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.create(HttpStorageRpc.java:219)
... 7 more
更新(20180309):
使用hard-coded Service Account Credentials运行以下代码,我可以访问Google存储并从中下载文件。
String SERVICE_ACCOUNT_JSON_PATH = "C:\\gcpconfig\\My First Project-6f9cff47c4f0.json";
Storage storage =
StorageOptions.newBuilder()
.setCredentials(
ServiceAccountCredentials.fromStream(
new FileInputStream(SERVICE_ACCOUNT_JSON_PATH)))
.build()
.getService();
BlobId blobId = BlobId.of("nmjcloud_jar_test","addons/simple-bean-1.0.jar");
Blob blob = storage.get(blobId);
Path path = Paths.get("D:\\lib\\simple-bean-1.0.jar");
blob.downloadTo(path);
System.out.printf("Download successfully%n");
不幸的是,将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为我的关键json文件的完整路径仍然不起作用。看来Google存储api库没有从
GOOGLE_APPLICATION_CREDENTIALS
读取密钥json文件。Oh dear. It's My Mistake
最佳答案
如java-docs-samples所述,您需要定义某种有效的凭据才能使代码正常工作。在本地计算机中,两个有效选项是:
设置GOOGLE_APPLICATION_CREDENTIALS环境变量,使其指向服务帐户密钥JSON文件路径(请确保不要用双引号/单引号引起来)
执行gcloud auth application-default login
鉴于您提到的the doc具有类似的说明,我建议您尝试选择选项2,同时检查以前的拼写错误,要访问的对象的权限...
注意:here您将找到如何创建JSON密钥文件以及here如何设置环境变量。