All Products
Search
Document Center

Simple Log Service:Initialize Simple Log Service SDK for Java

Last Updated:Jan 03, 2025

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.

  • The required endpoint is obtained.

    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.

    • Public endpoints: If data is pulled over a public Simple Log Service endpoint, read traffic over the Internet is generated. For more information about billing, see Billable items of pay-by-ingested-data and Billable items of pay-by-feature. For more information about endpoints, see Endpoints.

    • VPC endpoints: If the Elastic Compute Service (ECS) instance that you use and your Simple Log Service project reside in the same region, we recommend that you use a VPC endpoint. For more information, see Endpoints.

    • Acceleration endpoints: If your server and Simple Log Service reside in different regions, such as in and outside China, network latency is high and transmission is unstable when data is transmitted over the Internet. In this case, you can use an acceleration endpoint. For more information, see Use the transfer acceleration feature.

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.

cn-hangzhou.log.aliyuncs.com

accessKeyId

String

Yes

  • If you use an AccessKey pair to configure access credentials, set this parameter to the AccessKey ID of your Alibaba Cloud account or Resource Access Management (RAM) user. The AccessKey ID is used to identify the user. For more information, see Configure access credentials.

    Warning

    The AccessKey pair of an Alibaba Cloud account has full permissions on resources. We recommend that you do not use the AccessKey pair of an Alibaba Cloud account to avoid risks caused by AccessKey pair leaks. We recommend that you use the AccessKey pair of a RAM user that is granted permissions based on the principle of least privilege.

  • If you use Security Token Service (STS) to configure access credentials, set this parameter to the value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation.

LTAI5tQisap3nb5aDQ******

accessKeySecret

String

Yes

  • If you use an AccessKey pair to configure access credentials, set this parameter to the AccessKey secret of your Alibaba Cloud account or RAM user. The AccessKey secret is used to verify your AccessKey ID. For more information, see Configure access credentials.

  • If you use STS to configure access credentials, set this parameter to the value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation.

8IK3wR4CLBWVnhgY1DB1vttQ******

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