Initialize the SLS Go SDK

更新时间:
复制 MD 格式

The SLS Go SDK Client provides methods to create projects and Logstores, write logs, and read logs. Initialize a Client instance and modify the default configurations as needed before sending requests.

Prerequisites

Initialize a client

API reference

// 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 

Request parameters

Variable

Type

Required

Description

Example

endpoint

String

Yes

The SLS endpoint. Obtain an endpoint.

cn-hangzhou.log.aliyuncs.com

accessKeyID

String

Yes

  • If you use an AccessKey pair, set this to the AccessKey ID of your Alibaba Cloud account or RAM user. Configure access credentials.

    Warning

    An Alibaba Cloud account AccessKey pair has full resource permissions. To reduce the risk of AccessKey pair leaks, use a RAM user AccessKey pair with minimum required permissions.

  • If you use an STS token, set this to the AccessKeyId value in the Credentials returned by the AssumeRole operation.

LTAI****************

accessKeySecret

String

Yes

  • If you use an AccessKey pair, set this to the AccessKey secret of your Alibaba Cloud account or RAM user. Configure access credentials.

  • If you use an STS token, set this to the AccessKeySecret value in the Credentials returned by the AssumeRole operation.

yourAccessKeySecret

securityToken

String

No

  • Required only for STS tokens. Set this to the SecurityToken value in the Credentials returned by the AssumeRole operation.

  • STS token length is variable. Do not impose a maximum length limit. AssumeRole.

****************

Examples

Initialize with an AccessKey pair (V4 signature)

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")
}

Initialize with an AccessKey pair (V1 signature)

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)
}

Initialize with an STS token

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)
}

References

  • After initializing a client, call API operations to create projects, write logs, and more. Go SDK Quick Start.