Object Storage Service (OSS) erişim günlükleri, paketlerinize yapılan isteklere ilişkin ayrıntılı bilgileri yakalar. Bir kaynak paket için günlük kaydını etkinleştirdiğinizde OSS, önceden tanımlanmış bir adlandırma kuralı kullanarak saatlik olarak günlük dosyalarını otomatik oluşturur ve belirttiğiniz hedef pakete teslim eder.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerini yapılandırma hakkında daha fazla bilgi için bkz. OSS SDK for Python 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı gösterir. Özel alan adı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulaması yapmak gibi alternatif yapılandırmalar için bkz. Başlatma.
Bir paket için günlük kaydı yönetimi belirli izinler gerektirir:
oss:PutBucketLogging(etkinleştirmek için),oss:GetBucketLogging(sorgulamak için) veoss:DeleteBucketLogging(devre dışı bırakmak için). Daha fazla bilgi için bkz. Özel bir ilke atama.
Bir paket için günlük kaydını etkinleştirme
Aşağıdaki örnek kod, bir paket için günlük kaydının nasıl etkinleştirileceğini gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketLogging
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify that the generated log objects are stored in the current bucket.
# Set the directory in which the log objects are stored to log/. If you specify this parameter, the log objects are stored in the specified directory of the bucket. If you do not specify this parameter, the log objects are stored in the root directory of the bucket.
# 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)
Bir paketin günlük kaydı yapılandırmalarını sorgulama
Aşağıdaki örnek kod, bir paketin günlük kaydı yapılandırmalarının nasıl sorgulanacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# View the logging configurations of the bucket.
logging = bucket.get_bucket_logging()
print('TargetBucket={0}, TargetPrefix={1}'.format(logging.target_bucket, logging.target_prefix))
Bir paket için günlük kaydını devre dışı bırakma
Aşağıdaki örnek kod, bir paket için günlük kaydının nasıl devre dışı bırakılacağını gösterir:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
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)