Use the OSS SDK for Go 2.0 to create, lock, query, and manage time-based retention policies (WORM) on a bucket. Retention periods range from 1 day to 70 years.
Usage notes
The sample code uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. To access resources from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, see OSS regions and endpoints.Access credentials in the sample code are loaded from environment variables. For configuration instructions, see Configure access credentials.
API summary
| Operation | Method | State required |
|---|---|---|
| Create a retention policy | InitiateBucketWorm | — |
| Cancel a retention policy | AbortBucketWorm | InProgress |
| Lock a retention policy | CompleteBucketWorm | InProgress |
| Query a retention policy | GetBucketWorm | Any |
| Extend the retention period | ExtendBucketWorm | Locked |
Cancel a retention policy
Only a policy in the InProgress (unlocked) state can be cancelled. Call AbortBucketWorm to delete it.
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"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.AbortBucketWormRequest{
Bucket: oss.Ptr(bucketName),
}
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)
}