Le client est le composant du SDK Java pour Simple Log Service. Il fournit des méthodes pour créer des projets et des Logstores, écrire des journaux et les lire. Avant d'envoyer des requêtes via le SDK Java, initialisez une instance Client et adaptez la configuration par défaut selon vos besoins.
Prérequis
Vous avez terminé l'installation du SDK Java.
Procédure
Deux méthodes d'initialisation sont disponibles. Choisissez celle qui correspond à votre scénario :
Initialisation par AccessKey : une paire AccessKey est un identifiant permanent utilisable directement pour les appels API. Cette méthode convient aux environnements stables ne nécessitant pas de rotation fréquente des identifiants.
Initialisation par STS : Security Token Service (STS) génère des identifiants d'accès temporaires. Cette méthode est adaptée aux scénarios exigeant une autorisation dynamique et éphémère.
Initialiser à l'aide d'une paire AccessKey
Initialiser le client
public Client(String endpoint, String accessKeyId, String accessKeySecret)
Paramètres de la requête
|
Variable |
Type |
Obligatoire |
Description |
Valeur d'exemple |
|
endpoint |
String |
Oui |
L'endpoint utilisé pour accéder à Simple Log Service. Un endpoint est une URL qui spécifie le protocole d'accès, le nom d'hôte, le port et le chemin pour la communication entre le client et le service. Simple Log Service propose les types d'endpoints suivants :
|
|
|
accessKeyId |
String |
Oui |
|
LTAI |
|
accessKeySecret |
String |
Oui |
Si vous utilisez une paire AccessKey pour configurer les identifiants d'accès, il s'agit de l'AccessKey Secret d'un compte Alibaba Cloud ou d'un utilisateur RAM. L'AccessKey Secret permet d'authentifier l'AccessKey ID. Pour plus d'informations, consultez Configurer les identifiants d'accès. |
yourAccessKeySecret |
Exemple de code
L'algorithme de signature V4 offre une sécurité accrue grâce à un chiffrement et une signature plus complexes. L'algorithme de signature V1 est plus simple. Sélectionnez un algorithme de signature en fonction de vos exigences :
Signature V4
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);
}
}
Signature V1
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);
}
}
Initialiser à l'aide de STS
Initialiser le client
Client(String endpoint, CredentialsProvider credentialsProvider) ;
Paramètres de la requête
|
Variable |
Type |
Obligatoire |
Description |
Exemple |
|
endpoint |
String |
Oui |
L'endpoint utilisé pour accéder à Simple Log Service. Un endpoint est une URL qui spécifie le protocole d'accès, le nom d'hôte, le port et le chemin pour la communication entre le client et le service. Simple Log Service propose les types d'endpoints suivants :
|
|
|
accessKeyId |
String |
Oui |
Si vous utilisez STS pour configurer les identifiants d'accès, il s'agit de l'AccessKeyId issu du paramètre Credentials renvoyé par l'opération API AssumeRole. |
LTAI |
|
accessKeySecret |
String |
Oui |
Si vous utilisez STS pour configurer les identifiants d'accès, il s'agit de l'AccessKeySecret issu du paramètre Credentials renvoyé par l'opération API AssumeRole. |
yourAccessKeySecret |
|
securityToken |
String |
Oui |
Si vous utilisez STS pour configurer les identifiants d'accès, il s'agit du SecurityToken issu du paramètre Credentials renvoyé par l'opération API AssumeRole. |
|
Exemple de 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)
));
}
}
Références
Après avoir initialisé le client, vous pouvez appeler des opérations API pour créer des projets, écrire des journaux ou effectuer d'autres actions. Pour plus d'informations, consultez le Démarrage rapide du SDK Java.