Agentic Bucket (OSS SDK for Python 2.0)

Updated at:

This topic describes how to use the OSS SDK for Python 2.0 to create and manage Agentic Buckets, bind and unbind wildcard CNAMEs, and create Bucket Spaces and upload objects.

Usage notes

  • To use Agentic Bucket features, you must install alibabacloud-oss-v2 V1.4.0 or later.

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region as an example and uses the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mappings between the regions and endpoints supported by OSS, see Regions and endpoints.

  • When you use the alibabacloud_oss_v2.agentic module (AgenticBucketClient or BucketSpaceClient), you must set both of the following in the configuration: region and account_id. In the request, the bucket parameter is the bucket name prefix, and the SDK automatically resolves it to the full name. The full name of an Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr, and the full name of a Bucket Space is {prefix}-{account_id}-{region}-bs-apsr.

  • You can access a Bucket Space in either of the following ways: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix, and the SDK automatically resolves it to the full name. Or, use BucketSpaceHelper and the standard client oss.Client. First, call the to_bucket_name method to convert the name prefix to the full name, and then access the Bucket Space through the standard client. In this case, the bucket parameter in the request must be set to the full name. When you use BucketSpaceHelper, you must also set both of the following in the configuration: region and account_id.

  • CNAME-related operations (creating a CnameToken, getting a CnameToken, binding a CNAME, listing CNAMEs, and deleting a CNAME) are provided by the standard client oss.Client. In the request, the bucket parameter must be set to the full name of the Agentic Bucket, which is {prefix}-{account_id}-{region}-ab-apsr.

  • In wildcard CNAME requests, the domain or cname parameter must be set to the base domain name (without the *. prefix). The base domain name must contain at least three segments (for example, example.com is invalid, and foo.example.com is valid). Before you bind a wildcard CNAME, you must apply for a CnameToken for the base domain name and complete domain ownership verification. If you bind a certificate, the certificate must be a wildcard certificate that matches *.base-domain. For more information about the complete rules for wildcard CNAMEs, see PutCname.

Sample code

Create an Agentic Bucket

The following code creates an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: create an Agentic Bucket
parser = argparse.ArgumentParser(description="create agentic bucket sample")

# Define command-line arguments, including the required region, bucket name prefix, and account ID, 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')
parser.add_argument('--account_id', help='The account id.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object. AgenticBucketClient resolves
    # the bucket name prefix to the full name automatically: {prefix}-{account_id}-{region}-ab-apsr
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the Agentic Bucket client with the preceding configuration
    client = oss_agentic.AgenticBucketClient(cfg)

    # Create an Agentic Bucket with the storage class set to Standard and the data redundancy type set to LRS
    result = client.create_agentic_bucket(oss_agentic.models.CreateAgenticBucketRequest(
        bucket=args.bucket,  # The bucket name prefix
        create_agentic_bucket_configuration=oss_agentic.models.CreateAgenticBucketConfiguration(
            storage_class='Standard',  # The storage class
            data_redundancy_type='LRS',  # The data redundancy type
        ),
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Query Agentic Bucket information

The following code queries the information about a specified Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: query Agentic Bucket information
parser = argparse.ArgumentParser(description="get agentic bucket sample")

# Define command-line arguments, including the required region, bucket name prefix, and account ID, 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')
parser.add_argument('--account_id', help='The account id.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the Agentic Bucket client with the preceding configuration
    client = oss_agentic.AgenticBucketClient(cfg)

    # Query the information about a specified Agentic Bucket. request.bucket is the name prefix
    result = client.get_agentic_bucket(oss_agentic.models.GetAgenticBucketRequest(
        bucket=args.bucket,  # The bucket name prefix
    ))

    # Get and print the details of the Agentic Bucket
    info = result.agentic_bucket_info
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' name: {info.name},'  # The full name of the Agentic Bucket
          f' region: {info.region},'  # The region
          f' storage class: {info.storage_class},'  # The storage class
          f' status: {info.status},'  # The status
          )

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

List Agentic Buckets

The following code lists all Agentic Buckets of the current account in a specified region.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: list Agentic Buckets
parser = argparse.ArgumentParser(description="list agentic buckets sample")

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

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the Agentic Bucket client with the preceding configuration
    client = oss_agentic.AgenticBucketClient(cfg)

    # Create a ListAgenticBuckets paginator
    paginator = client.list_agentic_buckets_paginator()

    # Iterate page by page through all Agentic Buckets of the current account in the specified region
    for page in paginator.iter_page(oss_agentic.models.ListAgenticBucketsRequest()):
        for o in page.agentic_buckets:
            # Print the full name and storage class of each Agentic Bucket
            print(f'agentic bucket: {o.name}, {o.storage_class}')

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Delete an Agentic Bucket

The following code deletes a specified Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: delete an Agentic Bucket
parser = argparse.ArgumentParser(description="delete agentic bucket sample")

# Define command-line arguments, including the required region, bucket name prefix, and account ID, 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')
parser.add_argument('--account_id', help='The account id.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the Agentic Bucket client with the preceding configuration
    client = oss_agentic.AgenticBucketClient(cfg)

    # Delete a specified Agentic Bucket. request.bucket is the name prefix
    result = client.delete_agentic_bucket(oss_agentic.models.DeleteAgenticBucketRequest(
        bucket=args.bucket,  # The bucket name prefix
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Create a CnameToken (wildcard domain name)

The following code applies for a CnameToken for the base domain name of a wildcard CNAME on an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the script purpose: apply for a CnameToken for a wildcard domain name on an Agentic Bucket
parser = argparse.ArgumentParser(description="create cname token sample")

# Define command-line arguments, including the required region, bucket name prefix, account ID, and base domain name
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('--account_id', help='The account id.', required=True)
parser.add_argument('--domain', help='The base domain name of the wildcard domain, without the "*." prefix.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the OSS client with the preceding configuration. CNAME-related operations are provided by the standard client,
    # and the bucket parameter in the request must be set to the full name of the Agentic Bucket
    client = oss.Client(cfg)

    # The full name of the Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr
    agentic_bucket = f'{args.bucket}-{args.account_id}-{args.region}-ab-apsr'

    # Apply for a CnameToken for the base domain name. Set domain to the base domain name without the *. prefix,
    # Set is_wild_card to true to apply for a CnameToken for a wildcard CNAME
    result = client.create_cname_token(oss.CreateCnameTokenRequest(
            bucket=agentic_bucket,  # The full name of the Agentic Bucket
            bucket_cname_configuration=oss.BucketCnameConfiguration(
                cname=oss.Cname(
                    domain=args.domain,  # The base domain name, without the *. prefix
                    is_wild_card=True,  # Identifies a wildcard CNAME
                ),
            ),
    ))

    # Print the status code and other information of the operation result to confirm the request status and the CnameToken details
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' cname: {result.cname_token.cname if hasattr(result.cname_token, "cname") else "Not set"},'
          f' token: {result.cname_token.token if hasattr(result.cname_token, "token") else "Not set"},'
          f' expire time: {result.cname_token.expire_time.strftime("%Y-%m-%dT%H:%M:%S.000Z") if hasattr(result.cname_token, "expire_time") else "Not set"},'
          f' bucket: {result.cname_token.bucket if hasattr(result.cname_token, "bucket") else "Not set"},'
          )

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Get a CnameToken (wildcard domain name)

The following code gets the CnameToken of a wildcard CNAME on an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the script purpose: get the CnameToken of a wildcard domain name on an Agentic Bucket
parser = argparse.ArgumentParser(description="get cname token sample")

# Define command-line arguments, including the required region, bucket name prefix, account ID, and base domain name
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('--account_id', help='The account id.', required=True)
parser.add_argument('--domain', help='The base domain name of the wildcard domain, without the "*." prefix.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the OSS client with the preceding configuration. CNAME-related operations are provided by the standard client,
    # and the bucket parameter in the request must be set to the full name of the Agentic Bucket
    client = oss.Client(cfg)

    # The full name of the Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr
    agentic_bucket = f'{args.bucket}-{args.account_id}-{args.region}-ab-apsr'

    # Get the CnameToken of the base domain name. Set cname to the base domain name without the *. prefix,
    # Set wildcard to true to get the CnameToken of a wildcard CNAME
    result = client.get_cname_token(oss.GetCnameTokenRequest(
            bucket=agentic_bucket,  # The full name of the Agentic Bucket
            cname=args.domain,  # The base domain name, without the *. prefix
            wildcard=True,  # Identifies a wildcard CNAME
    ))

    # Print the status code and other information of the operation result to confirm the request status and the CnameToken details
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' cname: {result.cname_token.cname if hasattr(result.cname_token, "cname") else "Not set"},'
          f' token: {result.cname_token.token if hasattr(result.cname_token, "token") else "Not set"},'
          f' expire time: {result.cname_token.expire_time.strftime("%Y-%m-%dT%H:%M:%S.000Z") if hasattr(result.cname_token, "expire_time") else "Not set"},'
          f' bucket: {result.cname_token.bucket if hasattr(result.cname_token, "bucket") else "Not set"},'
          )

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Bind a wildcard CNAME

The following code binds a wildcard CNAME to an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the script purpose: bind a wildcard CNAME to an Agentic Bucket
parser = argparse.ArgumentParser(description="put cname sample")

# Define command-line arguments, including the required region, bucket name prefix, account ID, and base domain name
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('--account_id', help='The account id.', required=True)
parser.add_argument('--domain', help='The base domain name of the wildcard domain, without the "*." prefix.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the OSS client with the preceding configuration. CNAME-related operations are provided by the standard client,
    # and the bucket parameter in the request must be set to the full name of the Agentic Bucket
    client = oss.Client(cfg)

    # The full name of the Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr
    agentic_bucket = f'{args.bucket}-{args.account_id}-{args.region}-ab-apsr'

    # The SSL certificate parameters. A wildcard CNAME must use a wildcard certificate that matches *.base-domain
    cert_id = '493****-cn-hangzhou'  # The certificate ID
    certificate = '''-----BEGIN CERTIFICATE-----
MIIDhDCCAmwCCQCFs8ixARsyrDANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
**** -----END CERTIFICATE-----'''  # The SSL certificate content
    private_key = '''-----BEGIN CERTIFICATE-----
MIIDhDCCAmwCCQCFs8ixARsyrDANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
**** -----END CERTIFICATE-----'''  # The private key content

    # Bind a wildcard CNAME to an Agentic Bucket. Set domain to the base domain name without the *. prefix,
    # Set is_wild_card to true to bind a wildcard CNAME
    result = client.put_cname(oss.PutCnameRequest(
            bucket=agentic_bucket,  # The full name of the Agentic Bucket
            bucket_cname_configuration=oss.BucketCnameConfiguration(
                cname=oss.Cname(
                    domain=args.domain,  # The base domain name, without the *. prefix
                    is_wild_card=True,  # Identifies a wildcard CNAME
                    certificate_configuration=oss.CertificateConfiguration(
                        certificate=certificate,  # The SSL certificate content
                        private_key=private_key,  # The private key content
                        cert_id=cert_id,  # The certificate ID
                    ),
                ),
            ),
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

List CNAME records

The following code lists the CNAME records of an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the script purpose: list the CNAME records of an Agentic Bucket
parser = argparse.ArgumentParser(description="list cname sample")

# Define command-line arguments, including the required region, bucket name prefix, and account ID, 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')
parser.add_argument('--account_id', help='The account id.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the OSS client with the preceding configuration. CNAME-related operations are provided by the standard client,
    # and the bucket parameter in the request must be set to the full name of the Agentic Bucket
    client = oss.Client(cfg)

    # The full name of the Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr
    agentic_bucket = f'{args.bucket}-{args.account_id}-{args.region}-ab-apsr'

    # Send a request to list the CNAME records of a specified Agentic Bucket
    result = client.list_cname(oss.ListCnameRequest(
            bucket=agentic_bucket,  # The full name of the Agentic Bucket
    ))

    # Print the status code, request ID, and other information of the operation result to confirm the request status and the CNAME list details
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' bucket: {result.bucket},'  # The bucket name
          f' owner: {result.owner},'  # The bucket owner
          )

    # If CNAME records exist, iterate through and print the details of each CNAME record
    if result.cnames:
        for r in result.cnames:
            # A value of true for is_wild_card indicates that the record is a wildcard CNAME
            print(f'domain: {r.domain}, '
                  f'last modified: {r.last_modified}, '
                  f'is wild card: {r.is_wild_card}, '
                  f'status: {r.status}'
                  )

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Delete a wildcard CNAME

The following code deletes a wildcard CNAME from an Agentic Bucket.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the script purpose: delete a wildcard CNAME from an Agentic Bucket
parser = argparse.ArgumentParser(description="delete cname sample")

# Define command-line arguments, including the required region, bucket name prefix, account ID, and base domain name
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('--account_id', help='The account id.', required=True)
parser.add_argument('--domain', help='The base domain name of the wildcard domain, without the "*." prefix.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Initialize the OSS client with the preceding configuration. CNAME-related operations are provided by the standard client,
    # and the bucket parameter in the request must be set to the full name of the Agentic Bucket
    client = oss.Client(cfg)

    # The full name of the Agentic Bucket is {prefix}-{account_id}-{region}-ab-apsr
    agentic_bucket = f'{args.bucket}-{args.account_id}-{args.region}-ab-apsr'

    # Delete a wildcard CNAME from an Agentic Bucket. Set domain to the base domain name without the *. prefix,
    # Set is_wild_card to true to delete a wildcard CNAME
    result = client.delete_cname(oss.DeleteCnameRequest(
            bucket=agentic_bucket,  # The full name of the Agentic Bucket
            bucket_cname_configuration=oss.BucketCnameConfiguration(
                cname=oss.Cname(
                    domain=args.domain,  # The base domain name, without the *. prefix
                    is_wild_card=True,  # Identifies a wildcard CNAME
                ),
            ),
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Create a Bucket Space

The following code creates a Bucket Space under a specified Agentic Bucket. The SDK provides two ways to access a Bucket Space. The following samples demonstrate each way, and you can choose one as needed.

Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: create a Bucket Space
parser = argparse.ArgumentParser(description="bucket space put bucket sample")

# Define command-line arguments, including the required region, Bucket Space name prefix, account ID,
# the name prefix of the parent Agentic Bucket, 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 bucket space prefix name.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--agentic_bucket', help='The agentic bucket that the bucket space belongs to.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Create a Bucket Space client. This client reuses standard OSS bucket and object operations
    # and automatically resolves the Bucket Space name prefix to the full name {prefix}-{account_id}-{region}-bs-apsr
    client = oss_agentic.BucketSpaceClient.create(cfg)

    # Create a Bucket Space under a specified Agentic Bucket. The bucket parameter of PutBucketRequest
    # is the Bucket Space name prefix, and the agentic_bucket parameter is the full name of the parent Agentic Bucket
    result = client.put_bucket(oss.models.PutBucketRequest(
        bucket=args.bucket,  # The Bucket Space name prefix
        agentic_bucket=f'{args.agentic_bucket}-{args.account_id}-{args.region}-ab-apsr',  # The full name of the Agentic Bucket
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Method 2: Use BucketSpaceHelper and the standard client oss.Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: create a Bucket Space by using BucketSpaceHelper
parser = argparse.ArgumentParser(description="bucket space put bucket sample with helper")

# Define command-line arguments, the same as Method 1, including the required region, Bucket Space name prefix, account ID,
# the name prefix of the parent Agentic Bucket, 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 bucket space prefix name.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--agentic_bucket', help='The agentic bucket that the bucket space belongs to.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Create a standard OSS client
    client = oss.Client(cfg)

    # Create a BucketSpaceHelper. Based on the account ID and region in the configuration, the helper
    # converts the Bucket Space name prefix to the full name {prefix}-{account_id}-{region}-bs-apsr
    helper = oss_agentic.BucketSpaceHelper(cfg)

    # Create a Bucket Space under a specified Agentic Bucket. The bucket parameter of PutBucketRequest
    # is the full Bucket Space name converted by helper.to_bucket_name, and the agentic_bucket parameter is the full name of the parent Agentic Bucket
    result = client.put_bucket(oss.models.PutBucketRequest(
        bucket=helper.to_bucket_name(args.bucket),  # The full name of the Bucket Space
        agentic_bucket=f'{args.agentic_bucket}-{args.account_id}-{args.region}-ab-apsr',  # The full name of the Agentic Bucket
    ))

    # 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 run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Upload an object to a Bucket Space

The following code uploads an object to a specified Bucket Space. The following samples demonstrate the two access methods.

Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: upload an object to a Bucket Space
parser = argparse.ArgumentParser(description="bucket space put object sample")

# Define command-line arguments, including the required region, Bucket Space name prefix, account ID,
# the file 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 bucket space prefix name.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--key', help='The name of the object.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Create a Bucket Space client. This client reuses standard OSS bucket and object operations
    # and automatically resolves the Bucket Space name prefix to the full name {prefix}-{account_id}-{region}-bs-apsr
    client = oss_agentic.BucketSpaceClient.create(cfg)

    # Upload an object to a specified Bucket Space
    result = client.put_object(oss.models.PutObjectRequest(
        bucket=args.bucket,  # The Bucket Space name prefix
        key=args.key,  # The file name
        body=b'hello agentic bucket space',  # The file content
    ))

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

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()

Method 2: Use BucketSpaceHelper and the standard client oss.Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.agentic as oss_agentic

# Create a command-line argument parser and describe the script purpose: upload an object to a Bucket Space by using BucketSpaceHelper
parser = argparse.ArgumentParser(description="bucket space put object sample with helper")

# Define command-line arguments, the same as Method 1, including the required region, Bucket Space name prefix, account ID,
# the file 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 bucket space prefix name.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--key', help='The name of the object.', required=True)

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

    # Load access credentials from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object with the SDK default configuration and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region and account ID of the configuration object
    cfg.region = args.region
    cfg.account_id = args.account_id

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

    # Create a standard OSS client
    client = oss.Client(cfg)

    # Create a BucketSpaceHelper to convert the Bucket Space name prefix to the full name
    # {prefix}-{account_id}-{region}-bs-apsr
    helper = oss_agentic.BucketSpaceHelper(cfg)

    # Upload an object to a specified Bucket Space. The bucket parameter of PutObjectRequest
    # is the full Bucket Space name converted by helper.to_bucket_name
    result = client.put_object(oss.models.PutObjectRequest(
        bucket=helper.to_bucket_name(args.bucket),  # The full name of the Bucket Space
        key=args.key,  # The file name
        body=b'hello agentic bucket space',  # The file content
    ))

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

# When this script is run directly, call the main function to start the processing logic
if __name__ == "__main__":
    main()