Log storage (Android SDK)

更新时间:
复制 MD 格式

Object Storage Service (OSS) access logging captures detailed information about requests made to your buckets. After you enable logging for a source bucket, OSS will automatically generate and deliver log files hourly to a destination bucket you specify, using a predefined naming convention.

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 (Android SDK).

Note

After you call an API operation to enable or disable log storage, refresh the page to view the change. Switching tabs does not update the display.

Enable log storage

The following sample code enables log storage for a bucket.

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 sample code queries log storage configurations.

Note

On success, the output uses the `testBucket*path` format, where `testBucket` is the bucket name and `path` 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 sample code disables log storage for a bucket.

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