This topic describes how to determine whether a bucket exists.
Usage notes
The sample code in this topic uses the
cn-hangzhouregion ID for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the regions and endpoints for OSS, see Regions and endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure the access credentials, see Configure access credentials.
To determine whether a bucket exists, you must have the
oss:GetBucketAclpermission. For more information, see Attach a custom policy to a RAM user.
Method
func (c *Client) IsBucketExist(ctx context.Context, bucket string, optFns ...func(*Options)) (bool, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The context of the request, which can be used to specify the total duration of the request. |
bucket | string | The name of the bucket. |
optFns | ...func(*Options) | Optional. The operation-level parameter. For more information, see Options. |
Response parameters
Response parameter | Type | Description |
flag | bool | The response to the operation. This parameter is valid when the value of err is nil. |
err | error | The status of the request. If the request fails, the value of err cannot be nil. |
Examples
The following sample code provides an example on how to determine whether a bucket exists:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Specify the global variables.
var (
region string // The region.
bucketName string // The name of the bucket.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Determine whether the bucket exists.
result, err := client.IsBucketExist(context.TODO(), bucketName)
if err != nil {
log.Fatalf("failed to check if bucket exists %v", err)
}
// Display the result.
log.Printf("is bucket exist: %#v\n", result)
}
References
For the complete sample code that is used to determine whether a bucket exists, visit GitHub.
For more information about the API operation that you can call to determine whether a bucket exists, see IsBucketExist.