All Products
Search
Document Center

Object Storage Service:Resource groups (Go SDK V2)

Last Updated:Aug 02, 2025

This topic describes how to configure a resource group for a bucket and obtain the ID of the resource group to which the bucket belongs.

Usage notes

  • The sample code in this topic uses the cn-hangzhou region ID of the China (Hangzhou) region as an example. The public endpoint is used by default. If you want to access OSS from other Alibaba Cloud products in the same region, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.

  • This topic provides an example of obtaining access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • To configure a resource group for a bucket, you must have the oss:PutBucketResourceGroup permission. To query the resource group ID of a bucket, you must have the oss:GetBucketResourceGroup permission. For more information, see Attach a custom policy to a RAM user.

Method definitions

Configure the resource group for a bucket

func (c *Client) PutBucketResourceGroup(ctx context.Context, request *PutBucketResourceGroupRequest, optFns ...func(*Options)) (*PutBucketResourceGroupResult, error)

Obtain the ID of the resource group for a bucket

func (c *Client) GetBucketResourceGroup(ctx context.Context, request *GetBucketResourceGroupRequest, optFns ...func(*Options)) (*GetBucketResourceGroupResult, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request. You can use this parameter to set the total timeout period for the request.

request

*PutBucketResourceGroupRequest

The request parameters of the API operation. For more information, see PutBucketResourceGroupRequest

*GetBucketResourceGroupRequest

The request parameters of the API operation. For more information, see GetBucketResourceGroupRequest

optFns

...func(*Options)

Optional. The operation-level configuration parameters. For more information, see Options

Return values

Return value

Type

Description

result

*PutBucketResourceGroupResult

The return value of the API operation. This parameter is valid when err is nil. For more information, see PutBucketResourceGroupResult

*GetBucketResourceGroupRequest

The return value of the API operation. This parameter is valid when err is nil. For more information, see GetBucketResourceGroupResult

err

error

The status of the request. If the request fails, err is not nil.

Examples

Configure the resource group for a bucket

Important

If you do not specify a resource group ID when you configure a resource group for a bucket, the bucket is added to the default resource group. If you want to add the bucket to a specific resource group, make sure that the resource group is created. For more information, see Create a resource group.

The following sample code shows how to configure a resource group for a 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"
)

// Define global variables.
var (
	region     string // The region in which the bucket is located.
	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()

	// Specify the ID of the resource group. If you do not specify a resource group ID, the bucket is added to the default resource group.
	var groupId string = "rg-aekz****"

	// 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 set the resource group for the bucket.
	request := &oss.PutBucketResourceGroupRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		BucketResourceGroupConfiguration: &oss.BucketResourceGroupConfiguration{
			ResourceGroupId: oss.Ptr(groupId),
		},
	}

	// Send the request to set the resource group for the bucket.
	result, err := client.PutBucketResourceGroup(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket resource group %v", err)
	}

	// Print the result of setting the resource group for the bucket.
	log.Printf("put bucket resource group result:%#v\n", result)
}

Obtain the ID of the resource group for a bucket

The following sample code shows how to obtain the resource group ID of a 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"
)

// Define global variables.
var (
	region     string // The region in which the bucket is located.
	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 obtain the ID of the resource group for the bucket.
	request := &oss.GetBucketResourceGroupRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the operation to obtain the ID of the resource group for the bucket and process the result.
	result, err := client.GetBucketResourceGroup(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket resource group %v", err)
	}

	// Print the result of obtaining the ID of the resource group for the bucket.
	log.Printf("get bucket resource group id:%#v\n", *result.BucketResourceGroupConfiguration.ResourceGroupId)
}

References

  • For the complete sample code on how to configure a resource group for a bucket, see GitHub example.

  • For more information about the API operation for configuring a resource group for a bucket, see PutBucketResourceGroup.

  • For the complete sample code on how to obtain the resource group ID of a bucket, see GitHub example.

  • For more information about the API operation for obtaining the resource group ID of a bucket, see GetBucketResourceGroup.