All Products
Search
Document Center

Object Storage Service:Resource groups (Python SDK V1)

Last Updated:Aug 08, 2025

This topic describes how to configure a resource group for a bucket and obtain the bucket's 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 Initialization.

  • To configure a resource group for a bucket, 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 resource group for a bucket, the bucket is added to the default resource group if you do not specify a resource group ID. To add the bucket to a specific resource group, ensure that the resource group has been created. For more information, see Use resource groups.

The following code provides an example of how to configure a resource group for the bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the resource group ID. If you do not specify a resource group ID, the bucket is added to the default resource group.
resource_group_id = 'rg-aek27tc****'

# Configure the resource group for the bucket.
result = bucket.put_bucket_resource_group(resource_group_id)
print('The resource group is configured. The returned status is: ' + str(result.status))

Obtain the resource group ID of a bucket

The following code provides an example of how to obtain the resource group ID of the bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Obtain the resource group ID of the bucket.
result = bucket.get_bucket_resource_group()
print(result.resource_group_id)

References

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

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