All Products
Search
Document Center

Object Storage Service:Check whether a bucket exists (Go SDK V1)

Last Updated:Nov 28, 2025

A bucket is a container used to store objects in Object Storage Service (OSS). This topic describes how to determine whether a bucket exists.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

  • To determine whether a bucket exists, you must have the oss:GetBucketAcl permission. For more information, see Attach a custom policy to a RAM user.

  • Go SDK versions 2.2.5 and later support all properties that are returned in the following sample code.

Sample code

The following sample code demonstrates how to check whether a bucket exists.

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Replace yourRegion with the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Check whether the bucket exists.
	bucketName := "yourBucketName" // Replace the value with the actual bucket name.
	isExist, err := client.IsBucketExist(bucketName)
	if err != nil {
		log.Fatalf("Failed to check bucket existence: %v", err)
	}

	// Print the result.
	log.Printf("IsBucketExist result for '%s': %t", bucketName, isExist)
}

References

  • For more information about the API operation to check whether a bucket exists, see IsBucketExist.