All Products
Search
Document Center

Object Storage Service:Resource groups (Go SDK V1)

Last Updated:Nov 28, 2025

This topic describes how to configure a bucket's resource group and obtain its resource group ID.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an 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 access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

  • To configure a bucket's resource group, you must have the oss:PutBucketResourceGroup permission. To obtain a bucket's resource group ID, you must have the oss:GetBucketResourceGroup permission. For more information, see Grant custom access policies to a RAM user.

Configure the resource group for a bucket

Important

When you configure a bucket's resource group, the bucket is assigned to the default resource group if you do not specify a resource group ID. To add a bucket to a specific resource group, you must first create the resource group. For more information, see Create a resource group.

The following code shows how to configure the resource group for a bucket named examplebucket.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Replace yourRegion with the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	// Configure the resource group for the bucket.
	resourceGroup := oss.PutBucketResourceGroup{
		// Specify the resource group ID. If you do not specify a resource group ID, the bucket is assigned to the default resource group.
		ResourceGroupId: "rg-aekz****",
	}
	err = client.PutBucketResourceGroup(bucketName, resourceGroup)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Bucket Resource Group Set Success!")
}

Obtain the resource group ID of a bucket

The following code shows how to obtain the resource group ID for a bucket named examplebucket.

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual endpoint.
	// Replace yourRegion with the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the bucket name. For example, examplebucket.
	bucketName := "examplebucket"
	// Obtain the resource group ID of the bucket.
	result, err := client.GetBucketResourceGroup(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Printf("Resource Group Id:%s\n", result.ResourceGroupId)
}

References

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

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