All Products
Search
Document Center

Object Storage Service:Manage versioning

Last Updated:Feb 27, 2025

The versioning state of a bucket applies to all objects in the bucket. If you enable versioning for a bucket, you can recover any previous version of an object in the bucket after accidentally overwriting or deleting it.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by Object Storage Service (OSS), see OSS 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.

  • The oss:PutBucketVersioning permission is required to configure versioning for a bucket. The oss:GetBucketVersioning permission is required to query the versioning state of a bucket. For more information, see Grant Custom Permissions to RAM Users.

Methods

Configure the versioning state of a bucket

func (c *Client) PutBucketVersioning(ctx context.Context, request *PutBucketVersioningRequest, optFns ...func(*Options)) (*PutBucketVersioningResult, error)

Query the versioning state of a bucket

func (c *Client) GetBucketVersioning(ctx context.Context, request *GetBucketVersioningRequest, optFns ...func(*Options)) (*GetBucketVersioningResult, error)

Request parameters

Parameters

Types

Description

ctx

context.Context

The context of the request, which can be used to specify the total duration of the request.

request

*PutBucketVersioningRequest

The request parameter to configure the versioning of the bucket.

For more information, see PutBucketVersioningRequest.

*GetBucketVersioningRequest

The request parameter to query the versioning state of a bucket.

For more information, see GetBucketVersioningRequest.

optFns

...func(*Options)

Optional. The operation-level parameters. For more information, see Options.

Response parameters

Response parameters

Types

Description

result

*PutBucketVersioningResult

The response to the operation. This parameter is available when the value of err is nil. For more information, see PutBucketVersioningResult.

*GetBucketVersioningResult

The response to the operation. This parameter is available when the value of err is nil. For more information, see GetBucketVersioningResult.

err

error

The status of the request. If the request fails, the value of err is not nil.

Sample code

Configure the versioning state of a bucket

The following code provides an example of how to enable versioning for a bucket.

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"
)

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	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)

	// Create a request to configure the versioning state of the bucket.
	putRequest := &oss.PutBucketVersioningRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		VersioningConfiguration: &oss.VersioningConfiguration{
			Status: oss.VersionEnabled, // Enable versioning for the bucket.
		},
	}

	// Execute the request.
	putResult, err := client.PutBucketVersioning(context.TODO(), putRequest)
	if err != nil {
		log.Fatalf("failed to put bucket versioning %v", err)
	}

	// Display the result.
	log.Printf("put bucket versioning result:%#v\n", putResult)
}

Query the versioning state of a bucket

The following code provides an example of how to query the versioning state of a bucket.

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"
)

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "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 name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	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)

	// Create a request to query the versioning state of the bucket.
	getRequest := &oss.GetBucketVersioningRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
	}

	// Make the query request.
	getResult, err := client.GetBucketVersioning(context.TODO(), getRequest)
	if err != nil {
		log.Fatalf("failed to get bucket versioning %v", err)
	}

	// Display the result.
	log.Printf("get bucket versioning result:%#v\n", getResult)
}

References

  • For more information about the API operation that you can call to configure the versioning state of a bucket, see PutBucketVersioning.

  • For more information about the API operation that you can call to query the versioning state of a bucket, see GetBucketVersioning.