The Client is a Java SDK client that enables access to Simple Log Service. It offers a variety of methods to create projects, Logstores, write logs, and read logs. To initiate a request with the Simple Log Service SDK for Java, you must initialize a Client instance and adjust its default settings to meet your business needs.
Prerequisites
Procedure
Below are two methods for initialization. Choose the one that suits your requirements:
-
AccessKey pair initialization: This long-term valid pair can be used directly for API calls and is ideal for stable environments where credentials seldom change.
-
STS initialization: Temporary access credentials generated by STS are appropriate for scenarios requiring dynamic and temporary authorization.
Use an AccessKey pair
Initialize the client
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
Variable | Type | Required | Description | Example |
endpoint | String | Yes | Endpoint is the entry point for accessing Alibaba Cloud services. It is usually a URL that specifies the access protocol, hostname, port, and path of the service. The client can use this information to communicate with the service. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints:
|
|
accessKeyId | String | Yes |
|
|
accessKeySecret | String | Yes | If you use an AccessKey pair to configure the access credential, the AccessKey secret of the Alibaba Cloud account (primary account) and RAM user (sub-account) is used to authenticate the password of the AccessKey ID. For more information, see Configure access credentials. |
|
Sample code
The V4 signature algorithm provides enhanced security through more complex encryption and signature methods, while the V1 signature algorithm is simpler. Choose according to your security needs:
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 {
// The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
String endpoint = "cn-beijing.log.aliyuncs.com";
// In this example, the AccessKey ID and AccessKey secret are obtained 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 {
// The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
String endpoint = "cn-beijing.log.aliyuncs.com";
// In this example, the AccessKey ID and AccessKey secret are obtained 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 the client
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
Variable | Type | Required | Description | Example |
endpoint | String | Yes | Endpoint is the entry point for accessing Alibaba Cloud services. It is usually a URL that specifies the access protocol, hostname, port, and path of the service. The client can use this information to communicate with the service. Simple Log Service supports public endpoints, virtual private cloud (VPC) endpoints, and acceleration endpoints:
|
|
accessKeyId | String | Yes | If you use STS to configure the access credential, the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation is used. |
|
accessKeySecret | String | Yes | If you use STS to configure the access credential, the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation is used. |
|
Sample code
package com.test.controller;
import com.aliyun.openservices.log.Client;
public class Sample {
public static void main(String[] args) throws Exception {
// The endpoint of Simple Log Service. Beijing is used as an example. Specify other regions based on your actual situation.
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
-
Once the client is initialized, you can proceed with operations such as creating a project and writing logs. For more information, see the Quick Start for Java SDK.