All Products
Search
Document Center

Object Storage Service:Configure resource groups

Last Updated:Apr 02, 2025

This topic describes how to configure a resource group for an Object Storage Service (OSS) bucket and query the ID of a resource group to which the bucket belongs.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure the access credentials, see Configure access credentials.

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

Method

Configure a resource group for a bucket

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

Query the ID of the resource group to which a bucket belongs

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, which can be used to specify the total duration of the request.

request

*PutBucketResourceGroupRequest

The parameters of a specific API operation. For more information, visit PutBucketResourceGroupRequest.

*GetBucketResourceGroupRequest

The parameters of a specific API operation. For more information, visit GetBucketResourceGroupRequest.

optFns

...func(*Options)

Optional. The operation-level parameter. For more information, visit Options.

Response parameters

Parameter

Type

Description

result

*PutBucketResourceGroupResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, visit PutBucketResourceGroupResult.

*GetBucketResourceGroupRequest

The response to the operation. This parameter is valid when the value of err is nil. For more information, visit GetBucketResourceGroupResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Examples

Configure a resource group for a bucket

Important

If you do not specify a resource group ID, the bucket belongs 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 resource groups.

The following sample code provides an example on 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"
)

// 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()

	// Specify the ID of the resource group. If you do not specify a resource group ID, the bucket belongs 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 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 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),
		},
	}

	// Execute the request to configure 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)
	}

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

Query the ID of the resource group to which a bucket belongs

The following sample code provides an example on how to query the ID of the resource group to which a bucket belongs:

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 ID of the resource group to which the bucket belongs.
	request := &oss.GetBucketResourceGroupRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

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

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

References

  • For the complete sample code that is used to configure a resource group for a bucket, visit GitHub.

  • For more information about the API operation that you can call to configure a resource group for a bucket, visit PutBucketResourceGroup.

  • For the complete sample code that is used to query the ID of the resource group to which a bucket belongs, visit GitHub.

  • For more information about the API operation that you can call to query the ID of the resource group to which a bucket belongs, visit GetBucketResourceGroup.