您可以开启存储空间的访问日志记录功能,开启后对于此存储空间的访问会被记录成日志文件,保存在指定的存储空间中。

日志文件的格式为:

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

关于访问日志的更多信息,请参见开发指南中的日志转存

开启访问日志记录

以下代码用于开启存储空间的访问日志记录:

PutBucketLoggingRequest request = new PutBucketLoggingRequest();
//指定要开启访问日志记录的源Bucket。
request.setBucketName("<yourSourceBucketName>");
//指定存储访问日志的目标Bucket。
//目标Bucket和源Bucket必须属于同一地域。源Bucket和目标Bucket可以是同一个Bucket,也可以是不同的Bucket。
request.setTargetBucketName("<yourTargetBucketName>");
//设置日志文件存放的目录。
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();

开启存储空间访问日志记录的更多信息,请参见PutBucketLogging

查看访问日志设置

以下代码用于查看存储空间的访问日志设置:

GetBucketLoggingRequest request = new GetBucketLoggingRequest();
request.setBucketName("<yourSourceBucketName>");
OSSAsyncTask task = oss.asyncGetBucketLogging(request, new OSSCompletedCallback<GetBucketLoggingRequest, GetBucketLoggingResult>() {
    @Override
    public void onSuccess(GetBucketLoggingRequest request, GetBucketLoggingResult result) {
        OSSLog.logInfo("info: " + result.getTargetBucketName()+"*"+result.getTargetPrefix());

    }

    @Override
    public void onFailure(GetBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});
task.waitUntilFinished();

查看存储空间的访问日志配置的更多信息,请参见GetBucketLogging

关闭访问日志记录

以下代码用于关闭存储空间的访问日志记录:

DeleteBucketLoggingRequest request = new DeleteBucketLoggingRequest();
request.setBucketName("<yourSourceBucketName>");
OSSAsyncTask task = oss.asyncDeleteBucketLogging(request, new OSSCompletedCallback<DeleteBucketLoggingRequest, DeleteBucketLoggingResult>() {
    @Override
    public void onSuccess(DeleteBucketLoggingRequest request, DeleteBucketLoggingResult result) {
        OSSLog.logInfo("code::"+result.getStatusCode());

    }

    @Override
    public void onFailure(DeleteBucketLoggingRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});

task.waitUntilFinished();
			

关闭存储空间的访问日志记录功能的更多信息,请参见DeleteBucketLogging