A large number of logs are generated when Object Storage Service (OSS) resources are accessed. After you enable and configure logging for a bucket, OSS generates logs every hour in accordance with predefined naming conventions and then stores the logs as objects in a specified bucket.

Enable logging for a bucket

The following code provides an example on how to enable logging for a bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import BucketLogging

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket.     
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Specify that the generated log objects are stored in the current bucket. 
# Specify that the log objects are stored in the log/ directory. If you specify this parameter, log objects are stored in a specified directory of the destination bucket. If you do not specify this parameter, log objects are stored in the root directory of the destination bucket. To be specific, a directory named log/ is created in the root directory to store the log objects. 
# Enable logging for the bucket. 
logging = bucket.put_bucket_logging(BucketLogging(bucket.bucket_name, 'log/'))
if logging.status == 200:
    print("Enable access logging")
else:
    print("request_id:", logging.request_id)
    print("resp:", logging.resp.response)            

View the logging configurations of a bucket

The following code provides an example on how to view the logging configurations of a bucket:

# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket.     
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# View the logging configurations of the bucket. 
logging = bucket.get_bucket_logging()
print('TargetBucket={0}, TargetPrefix={1}'.format(logging.target_bucket, logging.target_prefix))            

Disable logging for a bucket

The following code provides an example on how to disable logging for a bucket:

# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket.     
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Disable logging for the bucket. 
logging = bucket.delete_bucket_logging()
if logging.status == 204:
    print("Disable access logging")
else:
    print("request_id:", logging.request_id)
    print("resp:", logging.resp.response)            

References

  • For the complete sample code that is used to configure logging for a bucket, visit GitHub.
  • For more information about the API operation that you can call to enable logging for a bucket, see PutBucketLogging.
  • For more information about the API operation that you can call to view the logging configurations of a bucket, see GetBucketLogging.
  • For more information about the API operation that you can call to disable logging for a bucket, see DeleteBucketLogging.