Client is the Java SDK client for Simple Log Service. It provides methods to create projects and Logstores, write logs, and read logs. Before you send requests by using the Java SDK, initialize a Client instance and modify the default configurations as needed.
Prerequisites
-
You have completed the Java SDK installation.
Procedure
Two initialization methods are available. Choose the one that fits your scenario:
-
AK initialization: An AccessKey pair is a long-term credential that can be directly used for API calls. This method is suitable for stable scenarios that do not require frequent credential rotation.
-
STS initialization: Security Token Service (STS) generates temporary access credentials. This method is suitable for scenarios that require dynamic, short-lived authorization.
Initialize using an AccessKey pair
Initialize the client
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Request parameters
|
Variable |
Type |
Required |
Description |
Example value |
|
endpoint |
String |
Yes |
The endpoint used to access Simple Log Service. An endpoint is a URL that specifies the access protocol, hostname, port, and path for client-to-service communication. Simple Log Service provides the following types of endpoints:
|
|
|
accessKeyId |
String |
Yes |
|
LTAI**************** |
|
accessKeySecret |
String |
Yes |
If you use an AccessKey pair to configure access credentials, the AccessKey secret of an Alibaba Cloud account or a RAM user. The AccessKey secret authenticates the AccessKey ID. For more information, see Configure access credentials. |
yourAccessKeySecret |
Sample code
The V4 signature algorithm offers higher security through more complex encryption and signing. The V1 signature algorithm is simpler. Select a signature algorithm based on your 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 {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// This example obtains the AccessKey ID and 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 {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// This example obtains the AccessKey ID and 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);
}
}
Initialize using STS
Initialize the client
Client(String endpoint, CredentialsProvider credentialsProvider) ;
Request parameters
|
Variable |
Type |
Required |
Description |
Example |
|
endpoint |
String |
Yes |
The endpoint used to access Simple Log Service. An endpoint is a URL that specifies the access protocol, hostname, port, and path for client-to-service communication. Simple Log Service provides the following types of endpoints:
|
|
|
accessKeyId |
String |
Yes |
If you use STS to configure access credentials, the AccessKeyId from the Credentials parameter returned by the AssumeRole API operation. |
LTAI**************** |
|
accessKeySecret |
String |
Yes |
If you use STS to configure access credentials, the AccessKeySecret from the Credentials parameter returned by the AssumeRole API operation. |
yourAccessKeySecret |
|
securityToken |
String |
Yes |
If you use STS to configure access credentials, the SecurityToken from the Credentials parameter returned by the AssumeRole API operation. |
**************** |
Sample code
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 {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Beijing) region. Replace it with the actual endpoint.
String endpoint = "cn-beijing.log.aliyuncs.com";
// This example obtains the AccessKeyId from the Credentials parameter returned by the AssumeRole operation from an environment variable.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
// This example obtains the AccessKeySecret from the Credentials parameter returned by the AssumeRole operation from an environment variable.
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// This example obtains the SecurityToken from the Credentials parameter returned by the AssumeRole operation from an environment variable.
String securityToken = System.getenv("ALIBABA_CLOUD_STS_TOKEN");
Client client = new Client(endpoint, new StaticCredentialsProvider(
new DefaultCredentials(accessKeyId, accessKeySecret, securityToken)
));
}
}
References
-
After you initialize the client, you can call API operations to create projects, write logs, or perform other operations. For more information, see Quick Start for Java SDK.