Todos os produtos
Search
Central de documentação

Simple Log Service:Inicializar o SLS Go SDK

Última atualização: Jul 03, 2026

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

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.

cn-hangzhou.log.aliyuncs.com

accessKeyID

String

Sim

  • Se usar um par de AccessKey, defina como o AccessKey ID da sua conta Alibaba Cloud ou usuário RAM. Configurar credenciais de acesso.

    Aviso

    O par de AccessKey da conta Alibaba Cloud tem permissões totais sobre os recursos. Para reduzir o risco de vazamento, use um par de AccessKey de usuário RAM com as permissões mínimas necessárias.

  • Se usar um token STS, defina como o valor AccessKeyId nas Credentials retornadas pela operação AssumeRole.

LTAI

accessKeySecret

String

Sim

  • Se usar um par de AccessKey, defina como o AccessKey secret da sua conta Alibaba Cloud ou usuário RAM. Configurar credenciais de acesso.

  • Se usar um token STS, defina como o valor AccessKeySecret nas Credentials retornadas pela operação AssumeRole.

yourAccessKeySecret

securityToken

String

Não

  • Obrigatório apenas para tokens STS. Defina como o valor SecurityToken nas Credentials retornadas pela operação AssumeRole.

  • O tamanho do token STS é variável. Não imponha limite máximo de comprimento. AssumeRole.

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