Tous les produits
Search
Centre de documentation

Object Storage Service:Manage bucket tagging using OSS SDK for Go 2.0

Dernière mise à jour :Aug 19, 2026

Cette rubrique explique comment gérer les tags d'un bucket.

Remarques sur l'utilisation

  • L'exemple de code de cette rubrique utilise l'ID de région cn-hangzhou correspondant à la région Chine (Hangzhou). Par défaut, l'accès aux ressources d'un bucket s'effectue via le point de terminaison public. Pour accéder aux ressources du bucket depuis d'autres services Alibaba Cloud situés dans la même région, utilisez le point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison Object Storage Service (OSS), consultez la page Régions et points de terminaison.

  • Dans cette rubrique, les identifiants d'accès sont récupérés depuis des variables d'environnement. Pour savoir comment configurer ces identifiants, reportez-vous à la section Configurer les identifiants d'accès.

Méthode

Configurer les tags d'un bucket

func (c *Client) PutBucketTags(ctx context.Context, request *PutBucketTagsRequest, optFns ...func(*Options)) (*PutBucketTagsResult, error)

Interroger les tags d'un bucket

func (c *Client) GetBucketTags(ctx context.Context, request *GetBucketTagsRequest, optFns ...func(*Options)) (*GetBucketTagsResult, error)

Supprimer les tags d'un bucket

func (c *Client) DeleteBucketTags(ctx context.Context, request *DeleteBucketTagsRequest, optFns ...func(*Options)) (*DeleteBucketTagsResult, error)

Paramètres de requête

Paramètre

Type

Description

ctx

context.Context

Contexte de la requête, permettant notamment de spécifier la durée totale de celle-ci.

request

*PutBucketTagsRequest

Spécifie les paramètres d'une opération API donnée. Pour plus de détails, consultez PutBucketTagsRequest.

*GetBucketTagsRequest

Spécifie les paramètres d'une opération API donnée. Pour plus de détails, consultez GetBucketTagsRequest.

*DeleteBucketTagsRequest

Spécifie les paramètres d'une opération API donnée. Pour plus de détails, consultez DeleteBucketTagsRequest.

optFns

...func(*Options)

Facultatif. Paramètre au niveau de l'opération. Pour plus de détails, consultez Options.

Paramètres de réponse

Paramètre

Type

Description

result

*PutBucketTagsResult

Réponse à l'opération. Ce paramètre est valide lorsque la valeur de err est nil. Pour plus de détails, consultez PutBucketTagsResult.

*GetBucketTagsRequest

Réponse à l'opération. Ce paramètre est valide lorsque la valeur de err est nil. Pour plus de détails, consultez GetBucketTagsResult.

*DeleteBucketTagsRequest

Réponse à l'opération. Ce paramètre est valide lorsque la valeur de err est nil. Pour plus de détails, consultez DeleteBucketTagsResult.

err

error

Statut de la requête. En cas d'échec, la valeur de err n'est pas nil.

Exemples

Configurer les tags d'un bucket

L'exemple suivant montre comment configurer les tags d'un 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The 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 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 specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to configure tags for the bucket.
	request := &oss.PutBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: &oss.Tagging{
			&oss.TagSet{
				[]oss.Tag{
					{
						Key:   oss.Ptr("k1"), // The key of a tag.
						Value: oss.Ptr("v1"), // The value of a tag.
					},
					{
						Key:   oss.Ptr("k2"), // The key of a tag.
						Value: oss.Ptr("v2"), // The value of a tag.
					},
					{
						Key:   oss.Ptr("k3"), // The key of a tag.
						Value: oss.Ptr("v3"), // The value of a tag.
					},
				},
			},
		},
	}

	// Send the request to configure tags for a bucket.
	result, err := client.PutBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket tags %v", err)
	}

	// Display the result of the tag configuration.
	log.Printf("put bucket tags result:%#v\n", result)
}

Interroger les tags d'un bucket

L'exemple suivant montre comment interroger les tags d'un 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The 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 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 specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to query the tags of the bucket.
	request := &oss.GetBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to query the tags of the bucket and process the result.
	getResult, err := client.GetBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket tags %v", err)
	}

	// Display the number of bucket tags.
	log.Printf("get bucket tags result:%#v\n", len(getResult.Tagging.TagSet.Tags))
}

Supprimer les tags d'un bucket

Supprimer un seul tag d'un bucket

L'exemple suivant montre comment supprimer le tag k1 d'un 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The 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 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 specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to delete a tag of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: oss.Ptr("k1"),
	}

	// Execute the request to delete the tag of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

Supprimer plusieurs tags d'un bucket

L'exemple suivant montre comment supprimer les tags k1 et k2 d'un 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The 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 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 specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to delete the two tags of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		Tagging: oss.Ptr("k1,k2"),
	}

	// Execute the request to delete the tags of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

Supprimer tous les tags d'un bucket

L'exemple suivant montre comment supprimer tous les tags d'un 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The 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 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 specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to delete all tags of the bucket.
	request := &oss.DeleteBucketTagsRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to delete all tags of the bucket and process the result.
	result, err := client.DeleteBucketTags(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket tags %v", err)
	}

	// Display the result of the tag deletion.
	log.Printf("delete bucket tags result:%#v\n", result)
}

Références

  • Pour en savoir plus sur l'opération API permettant de configurer les tags d'un bucket, consultez la page PutBucketTags.

  • Pour en savoir plus sur l'opération API permettant d'interroger les tags d'un bucket, consultez la page GetBucketTags.

  • Pour en savoir plus sur l'opération API permettant de supprimer les tags d'un bucket, consultez la page DeleteBucketTags.