全部產品
Search
文件中心

Object Storage Service:Android日誌轉存

更新時間:Feb 28, 2024

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

相關文檔