Client is a Java SDK client that you can use to access Simple Log Service. It provides various methods for you to create a project, create a Logstore, write logs, and read logs. To use Simple Log Service SDK for Java to initiate a request, you must initialize a Client instance and modify the default settings of the Client instance based on your business requirements.
Prerequisites
Simple Log Service SDK for Java is installed. For more information, see Install Simple Log Service SDK for Java.
The required access credentials are configured. For more information, see Configure access credentials.
Initialize a Client instance
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
Parameter | Type | Required | Description | Example |
endpoint | String | Yes | The endpoint. For more information, see Obtain an endpoint. |
|
accessKeyId | String | Yes |
|
|
accessKeySecret | String | Yes |
|
|
Examples
AccessKey pair-based initialization (V4 for signing)
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 {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// Obtain an AccessKey ID and an AccessKey secret from environment variables.
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);
}
}
AccessKey pair-based initialization (V1 for signing)
package com.test.controller;
import com.aliyun.openservices.log.Client;
public class Sample {
public static void main(String[] args) throws Exception {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// Obtain an AccessKey ID and an AccessKey secret from environment variables.
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-based initialization
package com.test.controller;
import com.aliyun.openservices.log.Client;
public class Sample {
public static void main(String[] args) throws Exception {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// In this example, obtain the value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
// In this example, obtain the value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation.
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// In this example, obtain the value of the SecurityToken parameter below the Credentials parameter that is returned by the AssumeRole operation.
String securityToken = System.getenv("ALIBABA_CLOUD_STS_TOKEN");
Client client = new Client(endpoint, accessKeyId, accessKeySecret);
client.setSecurityToken(securityToken);
}
}
References
After you initialize a Client instance, you can call Simple Log Service SDK for Java to create a project and write logs. For more information, see Get started with Simple Log Service SDK for Java.