Client, Simple Log Service için Java SDK istemcisidir. Projeler ve Logstore'lar oluşturma, günlük yazma ve günlük okuma yöntemleri sağlar. Java SDK'yı kullanarak istek göndermeden önce bir Client örneği başlatın ve varsayılan yapılandırmaları gerektiği gibi değiştirin.
Ön Koşullar
Java SDK kurulumu işlemini tamamladınız.
Yordam
İki başlatma yöntemi bulunur. Senaryonuz için uygun olanı seçin:
AK ile başlatma: AccessKey çifti, API çağrıları için doğrudan kullanılabilecek uzun vadeli bir kimlik bilgisidir. Bu yöntem, sık kimlik bilgisi döndürmesi gerektirmeyen kararlı senaryolar için uygundur.
STS ile başlatma: Security Token Service (STS), geçici erişim kimlik bilgileri oluşturur. Bu yöntem, dinamik ve kısa ömürlü yetkilendirme gerektiren senaryolar için uygundur.
AccessKey çifti kullanarak başlatma
İstemciyi başlatma
public Client(String endpoint, String accessKeyId, String accessKeySecret)
İstek parametreleri
|
Değişken |
Tür |
Gerekli |
Açıklama |
Örnek değer |
|
endpoint |
String |
Evet |
Simple Log Service'e erişmek için kullanılan uç nokta. Uç nokta, istemci-hizmet iletişimi için erişim protokolünü, ana bilgisayar adını, bağlantı noktasını ve yolu belirten bir URL'dir. Simple Log Service aşağıdaki uç nokta türlerini sağlar:
|
|
|
accessKeyId |
String |
Evet |
|
LTAI |
|
accessKeySecret |
String |
Evet |
Erişim kimlik bilgilerini yapılandırmak için bir AccessKey çifti kullanıyorsanız, bu alan bir Alibaba Cloud hesabının veya RAM kullanıcısının AccessKey gizli anahtarıdır. AccessKey gizli anahtarı, AccessKey ID'yi doğrular. Daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma. |
yourAccessKeySecret |
Örnek kod
V4 imza algoritması, daha karmaşık şifreleme ve imzalama sayesinde daha yüksek güvenlik sunar. V1 imza algoritması ise daha basittir. Gereksinimlerinize göre bir imza algoritması seçin:
V4 imzası
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 imzası
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);
}
}
STS kullanarak başlatma
İstemciyi başlatma
Client(String endpoint, CredentialsProvider credentialsProvider) ;
İstek parametreleri
|
Değişken |
Tür |
Gerekli |
Açıklama |
Örnek |
|
endpoint |
String |
Evet |
Simple Log Service'e erişmek için kullanılan uç nokta. Uç nokta, istemci-hizmet iletişimi için erişim protokolünü, ana bilgisayar adını, bağlantı noktasını ve yolu belirten bir URL'dir. Simple Log Service aşağıdaki uç nokta türlerini sağlar:
|
|
|
accessKeyId |
String |
Evet |
Erişim kimlik bilgilerini yapılandırmak için STS kullanıyorsanız, bu alan AssumeRole API işlemi tarafından döndürülen Credentials parametresinden alınan AccessKeyId değeridir. |
LTAI |
|
accessKeySecret |
String |
Evet |
Erişim kimlik bilgilerini yapılandırmak için STS kullanıyorsanız, bu alan AssumeRole API işlemi tarafından döndürülen Credentials parametresinden alınan AccessKeySecret değeridir. |
yourAccessKeySecret |
|
securityToken |
String |
Evet |
Erişim kimlik bilgilerini yapılandırmak için STS kullanıyorsanız, bu alan AssumeRole API işlemi tarafından döndürülen Credentials parametresinden alınan SecurityToken değeridir. |
|
Örnek kod
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)
));
}
}
İlgili Konular
İstemciyi başlattıktan sonra projeler oluşturmak, günlük yazmak veya diğer işlemleri gerçekleştirmek için API işlemlerini çağırabilirsiniz. Daha fazla bilgi için bkz. Java SDK Hızlı Başlangıç Kılavuzu.