Tous les produits
Search
Centre de documentation

Object Storage Service:Politiques de rétention avec OSS SDK for Go 2.0

Dernière mise à jour :Sep 22, 2026

Vous pouvez configurer une politique de rétention temporelle pour un bucket. La période de rétention varie d'un jour à 70 ans. Cette rubrique explique comment créer, interroger et verrouiller une politique de rétention.

Notes d'utilisation

  • L'exemple de code de cette rubrique utilise l'ID de région cn-hangzhou pour la région Chine (Hangzhou). Par défaut, un endpoint public permet d'accéder aux ressources d'un bucket. Pour accéder aux ressources d'un bucket depuis la même région via d'autres services Alibaba Cloud, utilisez un endpoint interne. Pour plus d'informations sur les régions et endpoints pris en charge par Object Storage Service (OSS), consultez Régions et endpoints OSS.

  • Dans cette rubrique, les identifiants d'accès proviennent de variables d'environnement. Pour plus d'informations sur la configuration des identifiants d'accès, consultez Configurer les identifiants d'accès.

Exemples de code

Créer une politique de rétention

Le code suivant crée une politique de rétention :

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

Annuler une politique de rétention non verrouillée

Le code suivant annule une politique de rétention non verrouillée :

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

Verrouiller une politique de rétention

Le code suivant verrouille une politique de rétention :

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

Interroger une politique de rétention

Le code suivant récupère la configuration de la politique de rétention :

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

Prolonger la période de rétention d'un objet

Le code suivant prolonge la période de rétention d'un objet dans une politique de rétention verrouillée :

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

Références

  • Pour plus d'informations sur la création d'une politique de rétention, consultez InitiateBucketWorm.

  • Pour plus d'informations sur l'annulation d'une politique de rétention non verrouillée, consultez AbortBucketWorm.

  • Pour plus d'informations sur le verrouillage d'une politique de rétention, consultez CompleteBucketWorm.

  • Pour plus d'informations sur l'interrogation d'une politique de rétention, consultez GetBucketWorm.

  • Pour plus d'informations sur la prolongation de la période de rétention d'un objet, consultez ExtendBucketWorm.