This topic describes how to use Log Service SDK for Go to create a Logstore that is used for hot and cold-tiered storage. This topic also provides sample code.

Prerequisites

  • The latest version of Log Service SDK for Go is installed. For more information, see Install Log Service SDK for Go.
  • The proto dependency is installed. You can run the go get -u github.com/gogo/protobuf/proto command to install the proto dependency.

Background information

Log Service provides the hot and cold-tiered storage feature. You can use the feature to reduce the costs of long-term storage without negative impacts on the queries, analysis, visualization, alerting, shipping, or transformation of log data.

For more information, see Enable hot and cold-tiered storage for a Logstore.

You can enable the hot and cold-tiered storage feature in the Log Service console. For more information, see Enable the hot and cold-tiered storage feature.

Sample code

In this example, a file named SLSColdLogstore.go is created. The sample code in this file shows how to call the CreateLogstore operation to create a Logstore that is used for hot and cold-tiered storage. Example:
package main
import (
    "fmt"
    "time"

    "github.com/aliyun/aliyun-log-go-sdk"
)
func main() {
    // Configure information such as the AccessKey pair, endpoint, project name, and Logstore name. 
    // The Log Service endpoint. For more information, see Endpoints. 
    // In this example, the Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    Endpoint := "cn-hangzhou.log.aliyuncs.com"
    // The AccessKey pair that is used to access Log Service. For more information, see AccessKey pair. An Alibaba Cloud account has permissions to call all API operations. If you use the AccessKey pair of an Alibaba Cloud account, security risks may occur. We recommend that you create and use a RAM user to call API operations or perform routine O&M. 
    AccessKeyId := "your_access_key_id"
    AccessKeySecret := "your_access_key_secret"
    // The Security Token Service (STS) token of the RAM user. If you leave this parameter empty, no STS tokens are used. For more information, see Assign a RAM role to an Alibaba Cloud account. 
    SecurityToken := ""
    // Create a Log Service client. 
    Client := sls.CreateNormalInterface(Endpoint, AccessKeyId, AccessKeySecret, SecurityToken)
    // The name of the project. 
    ProjectName := "aliyun-test-project"
    // The name of the Logstore. 
    LogStoreName := "aliyun-test-logstore"

    err := Client.CreateLogStore(ProjectName,LogStoreName,2,2,true,64)
    if err != nil {
        panic(err)
    }
    logstore, err := Client.GetLogStore(ProjectName, uLogStoreName)
    if err != nil {
        panic(err)
    }
    fmt.Println("create logstore successfully:", logstore.Name)
    // Change the period of time during which logs are stored in the hot storage tier of the Logstore to 61 days. 
    updateLogstore := &sls.LogStore{
        Name:LogStoreName,
        TTL:80,
        ShardCount:10,
        AutoSplit:false,
        WebTracking:true,
        HotTTL: 61,
    }
    err = Client.UpdateLogStoreV2(ProjectName,updateLogstore)
    if err != nil {
        panic(err)
    }
    fmt.Println("update logstore suecessed")
    fmt.Println("Prepare to delete the logstore after 30 seconds")
    time.Sleep(30 * time.Second)
    err = Client.DeleteLogStore(ProjectName, LogStoreName)
    if err != nil {
        panic(err)
    }
    fmt.Println("Delete Logstore successfully")
}
The following table describes the parameters that you must configure when you call the UpdateLogstoreV2 operation to update the Logstore for hot and cold-tiered storage.
Parameter Type Required Example Description
ProjectName String Yes aliyun-test-project The name of the project.

When you create a Log Service client, you must specify a value for the project_name parameter. Therefore, you do not need to set the parameter again.

LogStoreName String Yes aliyun-test-logstore The name of the Logstore.

When you create a Log Service client, you must specify a value for the logstore_name parameter. Therefore, you do not need to set the parameter again.

TTL Long Yes 3000 The maximum period of time during which logs can be stored. Unit: days. Valid values: 1 to 3650. If you set the value of this parameter to 3650, logs are permanently stored.
ShardCount Long Yes 2 The number of shards. Valid values: 1 to 10.
HotTTL String Yes 60 The period of time during which you want to store logs in the hot storage tier of the Logstore. Minimum value: 30. Unit: days. You can specify a value that ranges from 30 to the value of the TTL parameter.

When the period of time during which logs are stored in the hot storage tier of the Logstore exceeds the period of time specified by the HotTTL parameter, the logs are stored in the cold storage tier of the Logstore. For more information, see Enable hot and cold-tiered storage for a Logstore.