gpt4 book ai didi

java - com.google.cloud.storage.StorageException : 401 Unauthorized (from local machine)

转载 作者:行者123 更新时间:2023-11-30 06:16:51 25 4
gpt4 key购买 nike

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 环境变量设置为我的 key json 文件的完整路径仍然不起作用。谷歌存储 API 库似乎没有从 GOOGLE_APPLICATION_CREDENTIALS 读取 key json 文件。

<强> Oh dear. It's My Mistake

最佳答案

正如 java-docs-samples 中提到的,您需要定义某种有效的凭据才能使您的代码正常工作。在您的本地计算机中,两个有效选项是:

  1. 设置指向服务帐户 key JSON 文件路径的 GOOGLE_APPLICATION_CREDENTIALS 环境变量(确保不要用双引号/单引号括起来)
  2. 执行gcloud auth application-default登录

给定the doc您提到有类似的说明,我建议您尝试选项 2,同时检查以前的拼写错误、您尝试访问的对象的权限...

注:here您将了解如何创建 JSON key 文件和 here如何设置环境变量。

关于java - com.google.cloud.storage.StorageException : 401 Unauthorized (from local machine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49000925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com