O Client do SLS Go SDK fornece métodos para criar projetos e Logstores, além de gravar e ler logs. Antes de enviar solicitações, inicialize uma instância de Client e modifique as configurações padrão conforme necessário.
Pré-requisitos
O Go SDK do SLS está instalado.
-
Inicializar um client
Referência da API
// Initialize a client with an AccessKey pair.
func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken string) ClientInterface
// Initialize a client with custom access credentials.
func CreateNormalInterfaceV2(endpoint string, credentialsProvider CredentialsProvider) ClientInterface
Parâmetros da solicitação
|
Variável |
Tipo |
Obrigatório |
Descrição |
Exemplo |
|
endpoint |
String |
Sim |
Endpoint do SLS. Obter um endpoint. |
|
|
accessKeyID |
String |
Sim |
|
LTAI |
|
accessKeySecret |
String |
Sim |
|
yourAccessKeySecret |
|
securityToken |
String |
Não |
|
|
Exemplos
Inicializar com par de AccessKey (assinatura V4)
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Create a Simple Log Service client.
provider := sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")
client := sls.CreateNormalInterfaceV2(endpoint, provider)
// Set the signature version to v4.
client.SetAuthVersion(sls.AuthV4)
// Set the region.
client.SetRegion("cn-hangzhou")
}
Inicializar com par de AccessKey (assinatura V1)
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// This example obtains the AccessKey ID and AccessKey secret from environment variables.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Create a Simple Log Service client.
provider := sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")
client := sls.CreateNormalInterfaceV2(endpoint, provider)
}
Inicializar com token STS
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// This example obtains the AccessKeyId from the Credentials parameter of the AssumeRole response from an environment variable.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// This example obtains the AccessKeySecret from the Credentials parameter of the AssumeRole response from an environment variable.
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// This example obtains the SecurityToken from the Credentials parameter of the AssumeRole response from an environment variable.
securityToken := ""
// Create a Simple Log Service client.
provider := sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, securityToken)
client := sls.CreateNormalInterfaceV2(endpoint, provider)
}
Referências
Após inicializar o client, chame operações da API para criar projetos, gravar logs, entre outras tarefas. Consulte o Guia de início rápido do Go SDK.