To configure lifecycle rules based on the last access time, you must first enable access tracking. These rules allow you to automatically monitor access patterns of objects in an Object Storage Service (OSS) bucket and move cold data to more cost-effective 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or 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:PutBucketAccessMonitorpermission.To retrieve the access tracking status, you must have the
oss:GetBucketAccessMonitorpermission.
For more information, see Grant custom access policies to RAM users.
Enable access tracking
The following code provides an example of how to enable 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 code provides an example of how to query the access tracking status for 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
For more information about lifecycle rules based on last access time, see Lifecycle rules based on the last access time.
For more information about the API operation that you can call to enable access tracking, see PutBucketAccessMonitor.
For more information about the API operation that you can call to query the access tracking status, see GetBucketAccessMonitor.