All Products
Search
Document Center

Object Storage Service:Access tracking (Python SDK V2)

Last Updated:Mar 20, 2026

Use OSS Python SDK V2 to enable, disable, or query the access tracking status of a bucket.

The examples in this topic use the region ID cn-hangzhou (China (Hangzhou)) with a public endpoint. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For endpoint mappings, see OSS regions and endpoints.

Method definition

Enable access tracking

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

Request syntax:

result = client.put_bucket_access_monitor(
    oss.PutBucketAccessMonitorRequest(
        bucket='<your-bucket-name>',           # [REQUIRED] Name of the bucket
        access_monitor_configuration=oss.AccessMonitorConfiguration(
            status='Enabled' | 'Disabled',     # [REQUIRED] Target status
        ),
    )
)

Parameters:

ParameterTypeRequiredDescription
bucketstrYesName of the bucket
access_monitor_configurationAccessMonitorConfigurationYesConfiguration object containing the target status
access_monitor_configuration.statusstrYesAccess tracking status. Valid values: Enabled, Disabled

For the complete parameter reference, see PutBucketAccessMonitorRequest.

Return value:

FieldTypeDescription
status_codeintHTTP status code of the response
request_idstrUnique ID of the request, useful for troubleshooting

For the complete return value reference, see PutBucketAccessMonitorResult.

For the full method definition, see put_bucket_access_monitor.

Query the access tracking status

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

Request syntax:

result = client.get_bucket_access_monitor(
    oss.GetBucketAccessMonitorRequest(
        bucket='<your-bucket-name>',    # [REQUIRED] Name of the bucket
    )
)

Parameters:

ParameterTypeRequiredDescription
bucketstrYesName of the bucket

For the complete parameter reference, see GetBucketAccessMonitorRequest.

Return value:

result.status_code                              # HTTP status code
result.request_id                               # Request ID
result.access_monitor_configuration.status      # Current status: 'Enabled' or 'Disabled'

For the complete return value reference, see GetBucketAccessMonitorResult.

For the full method definition, see get_bucket_access_monitor.

Enable access tracking

The following example enables access tracking for a bucket. Credentials are loaded from environment variables.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put bucket access monitor sample")

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():
    args = parser.parse_args()

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

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    result = client.put_bucket_access_monitor(oss.PutBucketAccessMonitorRequest(
            bucket=args.bucket,
            access_monitor_configuration=oss.AccessMonitorConfiguration(
                status=args.status,
            ),
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id}')

if __name__ == "__main__":
    main()

For the complete sample, see put_bucket_access_monitor.py.

Query the access tracking status

The following example retrieves the current access tracking status of a bucket.

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="get bucket access monitor sample")

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():
    args = parser.parse_args()

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

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region

    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    result = client.get_bucket_access_monitor(oss.GetBucketAccessMonitorRequest(
            bucket=args.bucket,
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' status: {result.access_monitor_configuration.status},'
    )

if __name__ == "__main__":
    main()

For the complete sample, see get_bucket_access_monitor.py.