OSS generates access logs for each request made to resources in your buckets. After you enable logging on a bucket, OSS writes one log object per hour to a target bucket you specify, following a predefined naming convention.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket to enable logging on
A target bucket to store the generated log objects
The required RAM permissions: For instructions on granting these permissions, see Grant custom access policies to RAM users.
Operation Required permission Enable logging oss:PutBucketLoggingQuery logging configuration oss:GetBucketLoggingDisable logging oss:DeleteBucketLogging
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints.
The examples read credentials from environment variables. For setup instructions, see Configure access credentials using OSS SDK for Python 1.0.
The examples create an
OSSClientinstance with a standard OSS endpoint. For alternative configurations such as custom domains or Security Token Service (STS) authentication, see Initialization.
Enable logging for a bucket
Call put_bucket_logging with a BucketLogging object to specify the target bucket and an optional log prefix.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketLogging
# Load credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
# Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Required when using signature algorithm V4. Must match the endpoint region.
region = "cn-hangzhou"
# Replace with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Enable logging on the bucket.
# Log objects are written to the "log/" prefix in the target bucket.
# If you omit the prefix, log objects are stored in the root directory of the target 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)Query the logging configuration of a bucket
Call get_bucket_logging to retrieve the current logging configuration. The response includes target_bucket (where logs are stored) and target_prefix (the key prefix applied to log objects).
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Required when using signature algorithm V4.
region = "cn-hangzhou"
# Replace with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Retrieve and print the current logging configuration.
logging = bucket.get_bucket_logging()
print('TargetBucket={0}, TargetPrefix={1}'.format(logging.target_bucket, logging.target_prefix))Disable logging for a bucket
Call delete_bucket_logging to remove the logging configuration from a bucket. A successful response returns HTTP status 204.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Required when using signature algorithm V4.
region = "cn-hangzhou"
# Replace with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# 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)