【发布时间】:2017-11-15 10:20:04
【问题描述】:
我正在开发一个项目,其中包含将文件存储到 Google Cloud Storage 的过程。
最初的原型是用 Python 编写的,可以正常工作,但是在将代码库移植到 Java 时,我无法访问存储桶。
Java 项目是使用 Gradle 构建的,并且通过 NetBeans 进行开发。 ServiceAccount 是一个访问受限的密钥文件,只允许来自这个特定存储桶的读/写行为。
// The Java code, which should be enough to grab the bucket.
Storage storage = StorageOptions.getDefaultInstance().getService();
Bucket bucket = storage.get(BUCKET_NAME);
// For comparison, the Python setup.
storage_client = storage.Client()
self.bucket = storage_client.bucket(bucket_name)
从 Gradle 手动运行项目时,结果是,
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403
Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "xxx@xxx.iam.gserviceaccount.com does not have storage.buckets.get access to xxx.",
"reason" : "forbidden"
} ],
"message" : "xxx@xxx.iam.gserviceaccount.com does not have storage.buckets.get access to xxx."
}
从 NetBeans 本身运行时略有不同:
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Anonymous users does not have storage.buckets.get access to a1s-submissions.",
"reason" : "required"
} ],
"message" : "Anonymous users does not have storage.buckets.get access to a1s-submissions."
}
所有这些方法都返回了预期的结果 所以我至少知道文件正在被读取。
StorageOptions.getAppEngineAppId());
StorageOptions.getDefaultProjectId());
StorageOptions.CREDENTIAL_ENV_NAME);GOOGLE_APPLICATION_CREDENTIALS
StorageOptions.getDefaultInstance().getApplicationName());
StorageOptions.getDefaultInstance().getProjectId());
StorageOptions.getDefaultInstance().getCredentials()
.getAuthenticationType());
StorageOptions.getLibraryName());
因此,鉴于我可以通过 gcloud 和 python 访问存储桶,我知道该帐户具有授权,但是我似乎无法发现此错误的原因。
【问题讨论】:
-
类似的帖子,stackoverflow.com/questions/25768735/… 没有答案。
-
您要获取的内容是私有内容还是公共内容?如果它是私有内容,那么您需要在尝试获取资源之前创建资源的签名 URL。
-
私人内容。以前没有听说过签名的 URL……为什么 Python 库不需要这个?感谢您的领导,但没有想法。但是,当使用存储的密钥文件时,库应该只需要凭证的文件路径。否则,我也会让示例具有签名的 URL。
标签: java gradle netbeans google-cloud-storage