gpt4 book ai didi

java - 在 Google Cloud Storage 中创建存储桶有时会失败

转载 作者:行者123 更新时间:2023-12-02 01:34:21 27 4
gpt4 key购买 nike

我一直在尝试按照此 link 中的指南在 Google Cloud Storage 中创建存储桶。 ,使用Java。我有一个方法可以检查存储桶是否存在并返回一个 boolean 值,如下所示

private boolean checkIfBucketExists(String bucketName) {
return storage.get(bucketName, Storage.BucketGetOption.fields()) != null
}

还有另一种实际创建存储桶的方法,如下所示

public Bucket createBucket(String bucketName) {
if (checkIfBucketExists(bucketName)) {
System.out.println("Bucket already exists!");
}
return storage.create(
BucketInfo.newBuilder(bucketName)
// See here for possible values: http://g.co/cloud/storage/docs/storage-classes
.setStorageClass(StorageClass.COLDLINE)
// Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
.setLocation("eu")
.build());
}

当我创建存储桶时,有时它的行为有点奇怪。例如,我可以创建一个名为 name-bucketbucket,但不能创建 namebucket,即使我没有现有的我的项目中的 namebucket 。它给了我这个错误

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "myservice@project-id.iam.gserviceaccount.com does not have storage.buckets.get access to namebucket.",
"reason" : "forbidden"
} ],
"message" : "myservice@project-id.iam.gserviceaccount.com does not have storage.buckets.get access to namebucket."
}

在 Google Cloud Storage 中,我是否无法使用其他用户在其项目中选择的名称来创建存储桶?我无法选择名称 namebucket 的原因是因为其他人已经在他们的项目中命名了这样的存储桶吗?

最佳答案

是的,存储桶名称对于所有用户来说都是唯一的。

检查Documentation for bucket name considerations :

Bucket names reside in a single Cloud Storage namespace, which means that every bucket name must be unique. If you try to create a bucket with a name that is already assigned to an existing bucket, Cloud Storage responds with an error message. However, once you delete a bucket, its name can be reused by you or another user when creating a new bucket.

关于java - 在 Google Cloud Storage 中创建存储桶有时会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55455238/

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