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.
Procedure
You can select one of the following initialization methods based on your business requirements:
AccessKey pair-based initialization: AccessKey pairs are valid for a long period of time and can be directly used for API calls. AccessKey pair-based initialization is suitable for scenarios in which frequent access credential replacement is not required.
STS-based initialization: Security Token Service (STS) generates temporary access credentials. STS-based initialization is suitable for scenarios in which dynamic and temporary authorization is required.
Use an AccessKey pair
Initialize a Client instance
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
Parameter | Type | Required | Description | Example |
endpoint | String | Yes | The endpoint. An endpoint is used to access an Alibaba Cloud service. In most cases, an endpoint is a URL. An endpoint specifies information about a service, such as the access protocol, hostname, port, and path. Clients can use the information to access the service. For more information, see Endpoints. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints.
|
|
accessKeyId | String | Yes |
| LTAI**************** |
accessKeySecret | String | Yes | The AccessKey secret of your Alibaba Cloud account or RAM user if you use an AccessKey pair to configure access credentials. The AccessKey secret is used to verify your AccessKey ID. For more information, see Configure access credentials. | yourAccessKeySecret |
Sample code
The V4 signature algorithm uses more complex encryption and signature methods to provide higher security. The V1 signature algorithm is relatively simple. You can select a signature algorithm based on your business requirements.
V4 signature
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 (Beijing) region is used. Replace the parameter value with an 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);
}
}
V1 signature
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 (Beijing) region is used. Replace the parameter value with an 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);
}
}
Use STS
Initialize a Client instance
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
Parameter | Type | Required | Description | Example |
endpoint | String | Yes | The endpoint. An endpoint is used to access an Alibaba Cloud service. In most cases, an endpoint is a URL. An endpoint specifies information about a service, such as the access protocol, hostname, port, and path. Clients can use the information to access the service. For more information, see Endpoints. Simple Log Service supports public endpoints, VPC endpoints, and acceleration endpoints.
|
|
accessKeyId | String | Yes | The value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation if you use STS to configure access credentials. | LTAI**************** |
accessKeySecret | String | Yes | The value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation if you use STS to configure access credentials. | yourAccessKeySecret |
Sample code
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 (Beijing) region is used. Replace the parameter value with an 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 use 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.