Android日志转存

访问OSS的过程中会产生大量的访问日志。您可以通过日志转存功能将这些日志按照固定命名规则,以小时为单位生成日志文件写入您指定的存储空间(Bucket)。

注意事项

说明

调用日志转存开启或关闭接口成功后需要刷新页面才能看到成功开启转存,只是切换页签无法看到正确信息,请注意。

开启日志转存

以下代码用于开启日志转存功能。

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();

查看日志转存配置

以下代码用于查看日志转存配置。

说明

调用成功后输出结果格式为“testBucket*path”,其中"*"之前的"testBucket"为桶名,之后的"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) {
       // 请求异常。
              if (clientException != null) {
                  // 客户端异常,例如网络异常等。
                  clientException.printStackTrace();
              }
              if (serviceException != null) {
                  // 服务端异常。
                  Log.e("ErrorCode", serviceException.getErrorCode());
                  Log.e("RequestId", serviceException.getRequestId());
                  Log.e("HostId", serviceException.getHostId());
                  Log.e("RawMessage", serviceException.getRawMessage());
              }
    }
});
task.waitUntilFinished();

关闭日志转存

以下代码用于关闭日志转存功能。

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) {
        // 请求异常。
              if (clientException != null) {
                  // 客户端异常,例如网络异常等。
                  clientException.printStackTrace();
              }
              if (serviceException != null) {
                  // 服务端异常。
                  Log.e("ErrorCode", serviceException.getErrorCode());
                  Log.e("RequestId", serviceException.getRequestId());
                  Log.e("HostId", serviceException.getHostId());
                  Log.e("RawMessage", serviceException.getRawMessage());
              }
    }
});

task.waitUntilFinished();
            

相关文档