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
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.