Client is a Go SDK client that you can use to access Simple Log Service. It provides various methods for you to create a project, create a Logstore, write logs, and read logs. To use Simple Log Service SDK for Go to initiate a request, you must initialize a Client instance and modify the default settings of the Client instance based on your business requirements.
Prerequisites
Simple Log Service SDK for Go is installed. For more information, see Install Simple Log Service SDK for Go.
The required access credentials are configured. For more information, see Configure access credentials.
Initialize a Client instance
API operation
// Use an AccessKey pair to initialize a Client instance.
func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken string) ClientInterface
// Use custom access credentials to initialize a Client instance.
func CreateNormalInterfaceV2(endpoint string, credentialsProvider CredentialsProvider) ClientInterface Request parameters
Parameter | Type | Required | Description | Example |
endpoint | String | Yes | The endpoint. For more information, see Obtain an endpoint. |
|
accessKeyID | String | Yes |
| LTAI**************** |
accessKeySecret | String | Yes |
| yourAccessKeySecret |
securityToken | String | No |
| **************** |
Examples
AccessKey pair-based initialization (AuthV4 for signing)
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// Obtain an AccessKey ID and an 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)
// Use AuthV4 for signing.
client.SetAuthVersion(sls.AuthV4)
// Specify a region.
client.SetRegion("cn-hangzhou")
}AccessKey pair-based initialization (AuthV1 for signing)
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// Obtain an AccessKey ID and an 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)
}STS-based initialization
package main
import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"os"
)
func main() {
// Specify a Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint := "cn-hangzhou.log.aliyuncs.com"
// In this example, obtain the value of the AccessKeyId parameter below the Credentials parameter that is returned by the AssumeRole operation.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
// In this example, obtain the value of the AccessKeySecret parameter below the Credentials parameter that is returned by the AssumeRole operation.
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// In this example, obtain the value of the SecurityToken parameter below the Credentials parameter that is returned by the AssumeRole operation.
securityToken := ""
// Create a Simple Log Service client.
provider := sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, securityToken)
client := sls.CreateNormalInterfaceV2(endpoint, provider)
}References
After you initialize a Client instance, you can call Simple Log Service SDK for Go to create a project and write logs. For more information, see Get started with Simple Log Service SDK for Go.