Client是Log ServiceJava SDK的用戶端,它為調用者提供了一系列的方法,可以用來建立Project和LogStore、寫入日誌、讀取日誌等。使用Java SDK發起請求,您需要初始化一個Client執行個體,並根據需要修改預設配置項。
前提條件
操作步驟
以下提供兩種初始化方式,請按需選擇:
AK初始化:AK是長期有效,可直接用於API調用,適合不需要頻繁更換憑證的穩定情境。
STS初始化:STS產生的臨時訪問憑證,適用於需要動態、臨時授權的場合。
使用AK初始化
初始化Client
public Client(String endpoint, String accessKeyId, String accessKeySecret)請求參數
變數 | 類型 | 是否必填 | 說明 | 樣本值 |
endpoint | String | 是 | 服務入口(Endpoint)是訪問阿里雲服務的進入點,通常是一個URL,它指定了服務的訪問協議、主機名稱、連接埠和路徑等資訊,用戶端可以使用這些資訊與服務進行通訊。Log Service的Endpoint分為:
|
|
accessKeyId | String | 是 |
| LTAI**************** |
accessKeySecret | String | 是 | 使用AK配置訪問憑證,則為阿里雲帳號(主帳號)和RAM使用者(子帳號)的AccessKey Secret,用於驗證您擁有該AccessKey ID的密碼。具體參見配置訪問憑證。 | yourAccessKeySecret |
範例程式碼
V4簽名演算法採用更複雜的加密和簽名方式,提供了更高的安全性,V1簽名演算法相對比較簡單,可按需選擇:
V4簽名
package com.test.controller;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.http.client.ClientConfiguration;
import com.aliyun.openservices.log.http.signer.SignVersion;
public class Sample {
public static void main(String[] args) throws Exception {
// Log Service的服務存取點。此處以北京為例,其它地區請根據實際情況填寫
String endpoint = "cn-beijing.log.aliyuncs.com";
// 本樣本從環境變數中擷取 AccessKey ID 和 AccessKey Secret。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setRegion("cn-beijing");
clientConfiguration.setSignatureVersion(SignVersion.V4);
Client client = new Client(endpoint,
accessKeyId,
accessKeySecret,
clientConfiguration);
}
}V1簽名
package com.test.controller;
import com.aliyun.openservices.log.Client;
public class Sample {
public static void main(String[] args) throws Exception {
// Log Service的服務存取點。此處以北京為例,其它地區請根據實際情況填寫
String endpoint = "cn-beijing.log.aliyuncs.com";
// 本樣本從環境變數中擷取 AccessKey ID 和 AccessKey Secret。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
Client client = new Client(endpoint, accessKeyId, accessKeySecret);
}
}使用STS初始化
初始化Client
Client(String endpoint, CredentialsProvider credentialsProvider) ;請求參數
變數 | 類型 | 是否必填 | 說明 | 樣本值 |
endpoint | String | 是 | 服務入口(Endpoint)是訪問阿里雲服務的進入點,通常是一個URL,它指定了服務的訪問協議、主機名稱、連接埠和路徑等資訊,用戶端可以使用這些資訊與服務進行通訊。Log Service的Endpoint分為:
|
|
accessKeyId | String | 是 | 使用STS配置訪問憑證,則為AssumeRole介面返回參數Credentials中的AccessKeyId。 | LTAI**************** |
accessKeySecret | String | 是 | 使用STS配置訪問憑證,則為AssumeRole介面返回參數Credentials中的AccessKeySecret。 | yourAccessKeySecret |
securityToken | String | 是 | 使用STS配置訪問憑證,則為AssumeRole介面返回參數Credentials中的SecurityToken。 | **************** |
範例程式碼
package com.test.controller;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.auth.DefaultCredentials;
import com.aliyun.openservices.log.common.auth.StaticCredentialsProvider;
public class Sample {
public static void main(String[] args) throws Exception {
// Log Service的服務存取點。此處以北京為例,其它地區請根據實際情況填寫
String endpoint = "cn-beijing.log.aliyuncs.com";
// 本樣本從環境變數中擷取AssumeRole介面返回參數Credentials中的AccessKeyId。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
// 本樣本從環境變數中擷取AssumeRole介面返回參數Credentials中的AccessKeySecret。
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// 本樣本從環境變數中擷取AssumeRole介面返回參數Credentials中的SecurityToken。
String securityToken = System.getenv("ALIBABA_CLOUD_STS_TOKEN");
Client client = new Client(endpoint, new StaticCredentialsProvider(
new DefaultCredentials(accessKeyId, accessKeySecret, securityToken)
));
}
}相關文檔
初始化Client後,您可以調用介面實現建立Project、寫入日誌等操作,請參見Java SDK快速入門。