All Products
Search
Document Center

Object Storage Service:Retention policies with OSS SDK for Go 2.0

Last Updated:Dec 10, 2025

You can configure a time-based retention policy for a bucket. The retention period can range from one day to 70 years. This topic describes how to create, query, and lock a retention policy.

Usage notes

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

Sample code

Create a retention policy

The following code provides an example of how to create a retention policy:

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 // The region.
	bucketName string // The name of the bucket.
)

// The init function is 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 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 set 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 initialize the retention policy for the bucket.
	request := &oss.InitiateBucketWormRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket for which you want to configure the retention policy.
		InitiateWormConfiguration: &oss.InitiateWormConfiguration{
			RetentionPeriodInDays: oss.Ptr(int32(30)), // Specify that the retention period of the object is 30 days.
		},
	}

	// Initialize the retention policy for the bucket and process the result.
	result, err := client.InitiateBucketWorm(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to initiate bucket worm %v", err)
	}

	// Print the result of initializing the retention policy for the bucket.
	log.Printf("initiate bucket worm result:%#v\n", result)
}

Cancel an unlocked retention policy

The following code provides an example of how to cancel an unlocked retention policy:

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 // The region.
	bucketName string // The name of the bucket.
)

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

	// Configure the OSS client.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to cancel the retention policy.
	request := &oss.AbortBucketWormRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Cancel the retention policy and process the result.
	result, err := client.AbortBucketWorm(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to abort bucket worm %v", err)
	}

	log.Printf("abort bucket worm result:%#v\n", result)
}

Lock a retention policy

The following code provides an example of how to lock a retention policy:

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
	bucketName string
)

// The init function is 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() {
	var (
		wormId = "worm id" // wormId specifies the ID of the WORM task.
	)
	// 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 set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client using the configurations.
	client := oss.NewClient(cfg)

	// Create a request to complete the retention policy.
	request := &oss.CompleteBucketWormRequest{
		Bucket: oss.Ptr(bucketName),
		WormId: oss.Ptr(wormId),
	}
	// Call the CompleteBucketWorm method of the client to complete the retention policy.
	result, err := client.CompleteBucketWorm(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to complete bucket worm %v", err)
	}

	log.Printf("complete bucket worm result:%#v\n", result)
}

Query a retention policy

The following code provides an example of how to query a retention policy:

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 // The region.
	bucketName string // The name of the bucket.
)

// The init function is 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.")
}

// The main function.
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 set 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 retention policy configured for the bucket.
	request := &oss.GetBucketWormRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Query the retention policy configured for the bucket and process the result.
	result, err := client.GetBucketWorm(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket worm %v", err)
	}

	// Print the result of querying the retention policy configured for the bucket.
	log.Printf("get bucket worm result:%#v\n", result)
}

Extend the retention period of an object

The following code provides an example of how to extend the retention period of an object in a locked retention policy:

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 // The region.
	bucketName string // The name of the bucket.
)

// The init function is 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() {
	var (
		wormId = "worm id" // wormId specifies the ID of the WORM task.
	)
	// 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 set 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 extend the retention period specified in the retention policy.
	request := &oss.ExtendBucketWormRequest{
		Bucket: oss.Ptr(bucketName),
		WormId: oss.Ptr(wormId),
		ExtendWormConfiguration: &oss.ExtendWormConfiguration{
			RetentionPeriodInDays: oss.Ptr(int32(30)), // Extend the retention period of objects in the locked retention policy to 30 days.
		},
	}

	// Extend the retention period specified in the retention policy and process the result.
	result, err := client.ExtendBucketWorm(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to extend bucket worm %v", err)
	}

	// Print the result of extending the retention period specified in the retention policy.
	log.Printf("extend bucket worm result:%#v\n", result)
}

References

  • For more information about how to create a retention policy, see InitiateBucketWorm.

  • For more information about how to cancel an unlocked retention policy, see AbortBucketWorm.

  • For more information about how to lock a retention policy, see CompleteBucketWorm.

  • For more information about how to query a retention policy, see GetBucketWorm.

  • For more information about how to extend the retention period of an object, see ExtendBucketWorm.