All Products
Search
Document Center

Object Storage Service:Access tracking (Python SDK V2)

Last Updated:Aug 01, 2025

This topic describes how to use Python SDK V2 to configure the access tracking feature for a bucket.

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 from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.

Method definition

Enable access tracking

put_bucket_access_monitor(request: PutBucketAccessMonitorRequest, **kwargs) → PutBucketAccessMonitorResult

Query the access tracking status

get_bucket_access_monitor(request: GetBucketAccessMonitorRequest, **kwargs) → GetBucketAccessMonitorResult

Request parameters

Parameter

Type

Description

request

PutBucketAccessMonitorRequest

The request parameters. For more information, see PutBucketAccessMonitorRequest

GetBucketAccessMonitorRequest

The request parameters. For more information, see GetBucketAccessMonitorRequest

Return values

Type

Description

PutBucketAccessMonitorResult

The return value. For more information, see PutBucketAccessMonitorResult

GetBucketAccessMonitorResult

The return value. For more information, see GetBucketAccessMonitorResult

For the complete definition of enabling access tracking, see put_bucket_access_monitor.

For the complete definition of querying the access tracking status, see get_bucket_access_monitor.

Sample codes

Enable access tracking

The following code provides an example of how to enable access tracking for a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser and describe the purpose of the script: configure the access tracking status for a bucket.
parser = argparse.ArgumentParser(description="put bucket access monitor sample")

# Define command-line arguments, including the required region, bucket name, endpoint, and access tracking status.
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('--status', help='The access tracking status of the bucket. Valid values: Enabled, Disabled.', required=True)

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()

    # Create a configuration object using the default SDK configurations 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 command-line arguments provided by the user.
    cfg.region = args.region

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

    # Initialize the OSS client based on the preceding configurations to interact with OSS.
    client = oss.Client(cfg)

    # Send a request to configure the access tracking status of the specified bucket.
    result = client.put_bucket_access_monitor(oss.PutBucketAccessMonitorRequest(
            bucket=args.bucket,  # The name of the bucket.
            access_monitor_configuration=oss.AccessMonitorConfiguration(
                status=args.status,  # Set the new access tracking status.
            ),
    ))

    # 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 directly executed, call the main function to start the processing logic.
if __name__ == "__main__":
    main()  # The entry point of the script. The program flow starts from here.

Query the access tracking status

The following code provides an example of how to query the access tracking status of 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 access tracking status of a bucket.
parser = argparse.ArgumentParser(description="get bucket access monitor 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()

    # Create a configuration object using the default SDK configurations 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 command-line arguments provided by the user.
    cfg.region = args.region

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

    # Initialize the OSS client based on the preceding configurations to interact with OSS.
    client = oss.Client(cfg)

    # Execute a request to obtain the access tracking status of the specified bucket.
    result = client.get_bucket_access_monitor(oss.GetBucketAccessMonitorRequest(
            bucket=args.bucket,  # The name of the bucket.
    ))

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

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

References