When you access Object Storage Service (OSS), large numbers of access logs are generated. After you enable and configure logging for a bucket, OSS generates log objects every hour in accordance with predefined naming conventions and then stores the access logs as objects in a specified bucket. You can run the logging command to enable or disable logging for a bucket, and query the logging configurations of a bucket.
Sample command lines in this topic are based on the 64-bit Linux system. For other systems, replace ./ossutil64 in the commands with the corresponding binary name. For more information, see ossutil.
For more information about logging, see Log storage.
Enable logging for a bucket
If logging is not enabled for a bucket, you can run this command to enable logging for this bucket. If logging is enabled for this bucket, the existing logging configurations of this bucket are overwritten when you run this command.
Enable or modify logging configurations for a bucket by using the following command syntax:
Command syntax
./ossutil64 logging --method put oss://bucketname oss://targetbucket/[prefix]
The following table describes the parameters that you can configure when you run this command to enable logging for a bucket.
Parameter
Description
bucketname
The name of the source bucket for which log objects are generated.
targetbucket
The name of the destination bucket in which log objects are stored.
prefix
The directory in which log objects are stored. 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.
ImportantThe source bucket for which log objects are generated and the destination bucket where the generated log objects are stored can be the same bucket or different buckets. However, the destination bucket must belong to the same account in the same region as the source bucket.
Examples
Store log objects generated for the srcbucket source bucket in the root directory of the destbucket destination bucket.
./ossutil64 logging --method put oss://srcbucket oss://destbucket
Store log objects generated for the srcbucket source bucket in the destdir subdirectory of the root directory of the destbucket destination bucket.
./ossutil64 logging --method put oss://srcbucket oss://destbucket/destdir
If a similar output is displayed, logging is enabled for the srcbucket bucket:
0.098601(s) elapsed
View the logging configurations of a bucket
Command syntax
./ossutil64 logging --method get oss://bucketname [local_xml_file]
The following table describes the parameters that you can configure when you run this command to view the logging configurations of a bucket.
Parameter
Description
bucketname
The name of the bucket for which you want to query the logging configurations.
local_xml_file
The name of the local file used to store the logging configurations. Example:
localfile.txt
. If this parameter is not specified, obtained logging configurations are displayed without being stored in a local file.Examples
Query the logging configurations of the examplebucket bucket and write the obtained configuration results to the
localfile.txt
local file../ossutil64 logging --method get oss://examplebucket localfile.txt
If a similar output is displayed, you have obtained the logging configurations:
0.212407(s) elapsed
Query the logging configurations of the examplebucket bucket and display the results without storing these results in a local file.
./ossutil64 logging --method get oss://examplebucket
If a similar output is displayed, the log objects of the examplebucket bucket are stored in the root directory of the destbucket bucket:
<?xml version="1.0" encoding="UTF-8"?> <BucketLoggingStatus> <LoggingEnabled> <TargetBucket>destbucket</TargetBucket> <TargetPrefix></TargetPrefix> </LoggingEnabled> </BucketLoggingStatus> 0.109979(s) elapsed
Disable logging for a bucket
Before you disable logging, OSS keeps generating log objects. Delete log objects you no longer need to reduce storage costs based on lifecycle rules. For more information, see Lifecycle rules based on the last modified time.
Command syntax
./ossutil64 logging --method delete oss://bucketname
bucketname indicates the name of the bucket for which you want to disable logging.
Examples
Disable logging for the examplebucket bucket.
./ossutil64 logging --method delete oss://examplebucket
If a similar output is displayed, logging is disabled for the examplebucket bucket:
0.212409(s) elapsed
Common options
If you use ossutil to switch to a bucket that is located in another region, add the -e option to the command to specify the endpoint of the region in which the specified bucket is located. If you use ossutil to switch to a bucket that belongs to another Alibaba Cloud account, you can add the -i option to the command to specify the AccessKey ID of the specified account, and add the -k option to the command to specify the AccessKey secret of the specified account.
The following command provides an example on how to transfer log objects generated for the testbucket source bucket in the China (Hangzhou) region to the destbucket destination bucket:
./ossutil64 logging --method put oss://testbucket oss://destbucket -e oss-cn-hangzhou.aliyuncs.com -i LTAI4Fw2NbDUCV8zYUzA**** -k 67DLVBkH7EamOjy2W5RVAHUY9H****
For more information about other common options that you can use for the logging command, see Common options.