All Products
Search
Document Center

Object Storage Service:Access tracking

Last Updated:Mar 08, 2025

This topic explains how to set up access tracking for a bucket with the Python SDK V2.

Notes

  • The example code provided here uses the region ID cn-hangzhou for China (Hangzhou). The public endpoint is used by default. To access OSS through other Alibaba Cloud products within the same region, use the internal endpoint. For detailed information on the regions and endpoints supported by OSS, see OSS Regions and Endpoints.

Method definition

Enable access tracking

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

View access tracking status

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

Request parameter list

Parameter name

Type

Description

request

PutBucketAccessMonitorRequest

Set request parameters. For more information, see PutBucketAccessMonitorRequest

GetBucketAccessMonitorRequest

Set request parameters. For more information, see GetBucketAccessMonitorRequest

Return value list

Type

Description

PutBucketAccessMonitorResult

Return value. For more information, see PutBucketAccessMonitorResult

GetBucketAccessMonitorResult

Return value. For more information, see GetBucketAccessMonitorResult

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

For the full definition of viewing access tracking status, see get_bucket_access_monitor.

Sample code

Enable access tracking

Use the following code to enable access tracking for a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the script's purpose: set the access tracking status of 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 and obtain the values entered by the user
    args = parser.parse_args()

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

    # Create a configuration object using the SDK default configuration 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 using the above configuration to prepare for interaction 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,  # Bucket name
            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 executed directly, call the main function to start processing logic
if __name__ == "__main__":
    main()  # Script entry point, control program flow starts here

View access tracking status

Use the following code to check the access tracking status of a bucket:

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the script's purpose: 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, bucket name, and 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 and obtain the values entered by the user
    args = parser.parse_args()

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

    # Create a configuration object using the SDK default configuration 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 using the above configuration to prepare for interaction 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,  # Bucket name
    ))

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

References