edit-icon download-icon

Set logging

Last Updated: Aug 08, 2018

You can enable access logs to record bucket access to log files, which are stored in a specified bucket. The log file format is as follows:

<TargetPrefix><SourceBucket>-YYYY-mm-DD-HH-MM-SS-UniqueString

For more information about access log files, see Set access logging.

Enable access logging

Use the following code to enable bucket access logging:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // Create an OSSClient instance.
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. SetBucketLoggingRequest request = new SetBucketLoggingRequest("<yourSourceBucketName>");
  9. // Configure the bucket that stores log files.
  10. request.setTargetBucket("<yourTargetBucketName>");
  11. // Configure the log file storage directory.
  12. request.setTargetPrefix("<yourTargetPrefix>");
  13. ossClient.setBucketLogging(request);
  14. // Close your OSSClient.
  15. ossClient.shutdown();

View access logging configurations

Use the following code to view the access logging configurations for a bucket:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // Create an OSSClient instance.
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. BucketLoggingResult result = ossClient.getBucketLogging("<yourSourceBucketName>");
  9. System.out.println(result.getTargetBucket());
  10. System.out.println(result.getTargetPrefix());
  11. // Close your OSSClient.
  12. ossClient.shutdown();

Disable access logging

Use the following code to disable access logging for a bucket:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. // Create an OSSClient instance.
  7. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  8. SetBucketLoggingRequest request = new SetBucketLoggingRequest("<yourSourceBucketName>");
  9. request.setTargetBucket(null);
  10. request.setTargetPrefix(null);
  11. ossClient.setBucketLogging(request);
  12. // Close your OSSClient.
  13. ossClient.shutdown();
Thank you! We've received your feedback.