Manage versioning (Go SDK V1)

Updated at:

Versioning applies to all objects in a bucket. With versioning enabled, you can restore an object to any previous version after an accidental overwrite or deletion.

Prerequisites

Before you begin, make sure you have:

  • The oss:PutBucketVersioning permission to set the versioning state of a bucket

  • The oss:GetBucketVersioning permission to retrieve the versioning state of a bucket

  • For more information about granting permissions, see Grant custom permissions to a RAM user.

Usage notes

  • The examples in this topic use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For more information, see Regions and endpoints.

  • Access credentials in the examples are read from environment variables. For setup instructions, see Configure access credentials.

  • The examples create an OSSClient instance using an OSS endpoint. To use a custom domain name or Security Token Service (STS) instead, see Configure OSSClient instances.

Enable versioning for a bucket

The following example enables versioning for a bucket.

package main

import (
	"log"

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

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

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou.
	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)
	}

	// Create a bucket.
	// Set yourBucketName to the name of the bucket.
	bucketName := "yourBucketName"
	err = client.CreateBucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to create bucket '%s': %v", bucketName, err)
	}

	// Enable versioning for the bucket.
	config := oss.VersioningConfig{Status: "Enabled"}
	err = client.SetBucketVersioning(bucketName, config)
	if err != nil {
		log.Fatalf("Failed to set bucket versioning for '%s': %v", bucketName, err)
	}

	log.Printf("Bucket '%s' created and versioning enabled successfully", bucketName)
}

For the full API reference, see SetBucketVersioning.

Get the versioning state of a bucket

The following example retrieves and prints the versioning state of a bucket.

package main

import (
	"log"

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

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

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou.
	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)
	}

	// Get the versioning state of the bucket.
	// Set yourBucketName to the name of the bucket.
	bucketName := "yourBucketName"
	ret, err := client.GetBucketVersioning(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket versioning for '%s': %v", bucketName, err)
	}

	// Print the versioning state of the bucket.
	log.Printf("The bucket versioning status for '%s': %s", bucketName, ret.Status)
}

For the full API reference, see GetBucketVersioning.

List all object versions in a bucket

The following example lists all object versions and delete markers in a bucket, filtered by a prefix.

package main

import (
	"log"

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

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

	// Create an OSSClient instance.
	// Set yourEndpoint to the endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou.
	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)
	}

	// Set yourBucketName to the name of the bucket.
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// List object versions with a specific prefix.
	prefix := "yourObjectPrefix"
	ret, err := bucket.ListObjectVersions(oss.Prefix(prefix))
	if err != nil {
		log.Fatalf("Failed to list object versions for bucket '%s' with prefix '%s': %v", bucketName, prefix, err)
	}

	// Print object version information.
	for _, object := range ret.ObjectVersions {
		log.Printf("Object version: %v", object)
	}

	// Print delete marker information.
	for _, marker := range ret.ObjectDeleteMarkers {
		log.Printf("Delete marker: %v", marker)
	}
}

For the full API reference, see ListObjectVersions.