Access tracking (Python SDK V1)

Updated at:

Before you configure lifecycle rules based on last access time, you must enable access tracking. Access tracking monitors access patterns of objects in an Object Storage Service (OSS) bucket and identifies cold data that can be moved to lower-cost storage classes.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.

  • By default, Alibaba Cloud accounts have the permissions required for access tracking. If you use a Resource Access Management (RAM) user or STS to perform access tracking operations, you must have specific permissions. For example:

    • To enable access tracking, you must have the oss:PutBucketAccessMonitor permission.

    • To retrieve the access tracking status, you must have the oss:GetBucketAccessMonitor permission.

    For more information, see Grant custom access policies to RAM users.

Enable access tracking

The following example enables access tracking for a bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before running this code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the endpoint, for example, cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace "examplebucket" with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Enable access tracking for the bucket. After access tracking is enabled, if you want to change the status to Disabled, ensure that the bucket has no lifecycle rules based on the LastAccessTime match rule.
bucket.put_bucket_access_monitor("Enabled")

View the access tracking status

The following example queries the access tracking status of a bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Set endpoint to the endpoint of the region where the bucket is located. For example, use https://oss-cn-hangzhou.aliyuncs.com for the China (Hangzhou) region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set region to the region ID that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for Signature V4.
region = "cn-hangzhou"

# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Get the access tracking status of the bucket.
result = bucket.get_bucket_access_monitor()

# Print the access tracking status of the bucket.
print(result.access_monitor.status)

References