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
-
The SLS Go SDK is installed.
-
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. |
|
|
accessKeyID |
String |
Yes |
|
LTAI**************** |
|
accessKeySecret |
String |
Yes |
|
yourAccessKeySecret |
|
securityToken |
String |
No |
|
**************** |
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.