Google Storage:如何检查存储桶是否存在

ND0*_*003 6 go google-cloud-storage google-cloud-platform

使用 GoLang SDK 进行谷歌云存储......找不到检查存储桶是否存在的方法。

func (c *Client) Bucket(name string) *BucketHandle
Run Code Online (Sandbox Code Playgroud)

即使bucket不存在,Bucket也会返回一个BucketHandle。

那么,如何检查存储桶是否存在?如果桶不存在,我不想创建它,所以不能走尝试创建桶和处理错误的路线

F10*_*F10 5

这可以通过使用Attrs 函数来完成:

bucket := client.Bucket(bucketName)
exists,err := bucket.Attrs(ctx)
if err != nil {
    log.Fatalf("Message: %v",err)
}
fmt.Println(exists)
Run Code Online (Sandbox Code Playgroud)

因为err,打印Message: storage: bucket doesn't exist

如果您认为拥有直接提及存储桶是否存在的函数会很有用,我建议向 Cloud Storage 工程团队提交功能请求,以考虑将其用于后续版本。