The following sample code shows how to query the resource group ID of a bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser and describe the purpose of the script. The example describes how to query the resource group configurations of a bucket.
parser = argparse.ArgumentParser(description="get bucket resource group sample")
# Define the command-line arguments, including the required parameters — region and bucket name — as well as the optional endpoint parameter.
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 the command line arguments to obtain the values specified by the user.
args = parser.parse_args()
# Load access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration to create a cfg object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region attribute of the cfg object to the region provided in the command line.
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint attribute of the cfg object with the provided endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding settings to initialize the OSS Client instance.
client = oss.Client(cfg)
# Send a request to query the resource group of the bucket.
result = client.get_bucket_resource_group(oss.GetBucketResourceGroupRequest(
bucket=args.bucket, # Name of the bucket.
))
# Display the HTTP status code, request ID, and resource group ID to check the request status and details of configuration.
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")},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Entry point of the script. The control flow starts here.