All Products
Search
Document Center

Object Storage Service:Resource groups (Python SDK V2)

Last Updated:Aug 01, 2025

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

Notes

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

  • To configure the resource group for a bucket, you must have the oss:PutBucketResourceGroup permission. To obtain 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.

Sample code

Configure the resource group to which a bucket belongs

Important

If you do not specify a resource group ID when you configure the 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.

You can use the following code to configure the resource group for a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: Set the resource group configuration for a bucket.
parser = argparse.ArgumentParser(description="put bucket resource group sample")

# Define command-line arguments, including the required region and bucket name, and the optional endpoint and resource group ID.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--resource_group_id',
                    help='The ID of the resource group to which the bucket belongs. (Optional, default is an empty string)',
                    default='')

def main():
    # Parse command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the SDK default configurations to create a configuration object and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the provided command-line arguments.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property of the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSS client to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to set the resource group configuration for the specified bucket.
    result = client.put_bucket_resource_group(oss.PutBucketResourceGroupRequest(
            bucket=args.bucket,  # The name of the bucket.
            bucket_resource_group_configuration=oss.BucketResourceGroupConfiguration(
                resource_group_id=args.resource_group_id,  # The ID of the resource group.
            ),
    ))

    # Print the status code and request ID of the operation result to confirm the request status.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          )

# When this script is executed directly, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts here.

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

You can use the following code to obtain the ID of the resource group for a bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: Obtain the resource group configuration of a bucket.
parser = argparse.ArgumentParser(description="get bucket resource group sample")

# Define command-line arguments, including the required region and bucket name, and the optional endpoint.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')

def main():
    # Parse command-line arguments to obtain the values entered by the user.
    args = parser.parse_args()

    # Load access credential information from environment variables for identity verification.
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the SDK default configurations to create a configuration object and set the authentication provider.
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the provided command-line arguments.
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property of the configuration object.
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Use the preceding configuration to initialize the OSS client to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to obtain the resource group configuration of the specified bucket.
    result = client.get_bucket_resource_group(oss.GetBucketResourceGroupRequest(
            bucket=args.bucket,  # The name of the bucket.
    ))

    # Print the status code, request ID, and resource group ID of the operation result to confirm the request status and configuration details.
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' resource group id: {getattr(result.bucket_resource_group_configuration, "resource_group_id", "Not set")},'
          )

# When this script is executed directly, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts here.

References