The Python SDK provides a log recording feature to facilitate troubleshooting. This feature is disabled by default.
The Python SDK logging feature requires Python SDK version 2.6.x or later.
Background information
The Python SDK logging feature collects logs for various OSS operations and stores them in local log files.
Log format:
<time><name><level><threadId><message>Log levels: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET
NoteIf you specify a log level, the local log file records logs for that level and all higher levels. For example, if you set the log level to INFO, the log file records logs for the CRITICAL, ERROR, WARNING, and INFO levels.
Usage 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, 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.
Enable logging for the Python SDK
The following code enables logging for the Python software development kit (SDK).
# -*- coding: utf-8 -*-
import os
import logging
import oss2
from itertools import islice
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Download log information to a local log file and save it to a specified local path.
# If you do not specify a full path and provide only a file name, such as examplelogfile.log, the log file is saved to the project's root folder by default.
log_file_path = "D:\\localpath\\examplelogfile.log"
# Enable logging.
oss2.set_file_logger(log_file_path, 'oss2', logging.INFO)
# Obtain access credentials from environment variables. Before you run this code, make sure you have set the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set the endpoint to the one that corresponds to the region where your bucket is located. For example, if your 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, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace examplebucket with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Traverse the file directory.
for b in islice(oss2.ObjectIterator(bucket), 10):
print(b.key)
# Get the file metadata.
# Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot include the bucket name.
object_meta = bucket.get_object_meta('exampledir/exampleobject.txt')The following is the log output:
For more detailed logs, change the log level to DEBUG.
2018-11-20 16:37:46,437 oss2.api [INFO] 26716 : Exception: {
'status': 404,
'x-oss-request-id': '5BF3C7DA236B3A201CE64679',
'details': {
'HostId': 'examplebucket.oss-cn-shenzhen.aliyuncs.com',
'Message': 'The specified key does not exist.',
'Code': 'NoSuchKey',
'RequestId': '5BF3C7DA236B3A201CE64679',
'Key': 'exampledir/exampleobject.txt'
}
}The log shows that a NoSuchKey error occurred because the requested object does not exist.