All Products
Search
Document Center

Simple Log Service:Use Simple Log Service SDK for Go to create a Logstore for which intelligent tiered storage is enabled

Last Updated:Oct 26, 2023

This topic describes how to use Simple Log Service SDK for Go to create a Logstore for which intelligent tiered storage is enabled.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Go is installed. For more information, see Install Simple 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

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

You can enable the intelligent tiered storage feature in the Simple Log Service console. For more information, see Enable the intelligent tiered storage feature.

Sample code

In this example, a file named SLSColdLogstore.go is created. The sample code in this file provides an example on how to call the CreateLogStore operation to create a Logstore for which intelligent tiered storage is enabled. Example:

package main
import (
    "fmt"
    "os"
    "time"

    "github.com/aliyun/aliyun-log-go-sdk"
)
func main() {
    // The 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, the AccessKey ID and AccessKey secret are obtained from environment variables. 
    AccessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    AccessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    // The Security Token Service (STS) token of the Resource Access Management (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 Simple 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 which intelligent tiered storage is enabled.

Parameter

Type

Required

Example

Description

ProjectName

String

Yes

aliyun-test-project

The name of the project.

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

LogStoreName

String

Yes

aliyun-test-logstore

The name of the Logstore.

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

TTL

Long

Yes

3000

The retention period of data in the Logstore. Unit: days. Valid values: 1 to 3000.

Warning

After the retention period ends, log data is automatically deleted.

ShardCount

Long

Yes

2

The number of shards. Valid values: 1 to 10.

HotTTL

String

Yes

60

The retention period of data in the hot storage tier of the Logstore. Unit: days. Valid values: 7 to 3000.

Hot data that is stored for longer than the retention period specified by the HotTTL parameter is moved to the Infrequent Access storage tier (formerly known as the cold storage tier). For more information, see Enable the intelligent tiered storage feature.