Manage versioning using OSS SDK for Go 2.0

Updated at:
Copy as MD

Versioning applies to all objects in a bucket. After you enable versioning, you can recover any previous version of an object that was accidentally overwritten or deleted.

Notes

  • The sample code uses the region ID cn-hangzhou of the China (Hangzhou) region and the public endpoint by default. To access resources in a bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about supported regions and endpoints, see OSS Regions and Endpoints.

  • The sample code obtains access credentials 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 request context. You can use this parameter to specify the total request duration.

request

*PutBucketVersioningRequest

The request parameters for configuring bucket versioning.

For more information, see PutBucketVersioningRequest.

*GetBucketVersioningRequest

The request parameters for querying 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. Available when err is nil. For more information, see PutBucketVersioningResult.

*GetBucketVersioningResult

The response. Available when err is nil. For more information, see GetBucketVersioningResult.

err

error

The error information. A non-nil value indicates a failed request.

Sample code

Configure the versioning state of a bucket

The following example shows 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 example shows 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