Object Storage Service (OSS) generates access logs to record access to resources stored in OSS buckets. After you enable and configure logging for a bucket, OSS generates access logs every hour based on predefined naming rules and then stores the logs in a specific bucket.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
After you call an API operation to enable or disable log storage, refresh the page to view the change. The updated configuration does not appear if you only switch tabs.
Enable log storage
The following code shows how to enable the log storage feature.
PutBucketLoggingRequest request = new PutBucketLoggingRequest();
// Specify the source bucket for which to enable access logging.
request.setBucketName("yourSourceBucketName");
// Specify the destination bucket to store access logs.
// The destination bucket and the source bucket must be in the same region. The source and destination buckets can be the same or different.
request.setTargetBucketName("yourTargetBucketName");
// Set the folder where the log files are stored.
request.setTargetPrefix("<yourTargetPrefix>");
OSSAsyncTask task = oss.asyncPutBucketLogging(request, new OSSCompletedCallback<PutBucketLoggingRequest, PutBucketLoggingResult>() {
@Override
public void onSuccess(PutBucketLoggingRequest request, PutBucketLoggingResult result) {
OSSLog.logInfo("code::"+result.getStatusCode());
}
@Override
public void onFailure(PutBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
OSSLog.logError("error: "+serviceException.getRawMessage());
}
});
task.waitUntilFinished();View log storage configurations
The following code shows how to view log storage configurations.
If the call is successful, the output is in the `testBucket*path` format. In this format, `testBucket` before the asterisk (*) is the bucket name, and `path` after the asterisk is the storage path.
GetBucketLoggingRequest request = new GetBucketLoggingRequest();
request.setBucketName("yourSourceBucketName");
OSSAsyncTask task = oss.asyncGetBucketLogging(request, new OSSCompletedCallback<GetBucketLoggingRequest, GetBucketLoggingResult>() {
@Override
public void onSuccess(GetBucketLoggingRequest request, GetBucketLoggingResult result) {
Log.i("i", "info: " + result.getTargetBucketName()+"*"+result.getTargetPrefix());
}
@Override
public void onFailure(GetBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
// Request error.
if (clientException != null) {
// Client exception, such as a network error.
clientException.printStackTrace();
}
if (serviceException != null) {
// Server exception.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
task.waitUntilFinished();Disable log storage
The following code shows how to disable the log storage feature.
DeleteBucketLoggingRequest request = new DeleteBucketLoggingRequest();
request.setBucketName("yourSourceBucketName");
OSSAsyncTask task = oss.asyncDeleteBucketLogging(request, new OSSCompletedCallback<DeleteBucketLoggingRequest, DeleteBucketLoggingResult>() {
@Override
public void onSuccess(DeleteBucketLoggingRequest request, DeleteBucketLoggingResult result) {
Log.i("i", "code:"+result.getStatusCode());
}
@Override
public void onFailure(DeleteBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
// Request error.
if (clientException != null) {
// Client exception, such as a network error.
clientException.printStackTrace();
}
if (serviceException != null) {
// Server exception.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
task.waitUntilFinished();
References
For the complete sample code for log storage, see GitHub example.
For more information about the API operation that you can call to enable logging for a bucket, see PutBucketLogging.
For more information about the API operation that you can call to query the logging configurations of a bucket, see GetBucketLogging.
For more information about the API operation that you can call to disable logging for a bucket, see DeleteBucketLogging.
For more information about how to initialize an OSSClient instance, see How to initialize an OSSClient instance for Android.