All Products
Search
Document Center

Simple Log Service:Create a tiered storage LogStore using Go SDK

Last Updated:Feb 10, 2026

This topic provides a code example for creating a tiered storage LogStore using Go SDK.

Prerequisites

You have completed the following operations:

  • Simple Log Service SDK for Go is installed. For more information, see Install Go SDK.

  • You have installed the protobuf dependency package. Run the following command to install it: go get -u github.com/gogo/protobuf/proto.

Background information

Simple Log Service provides the intelligent tiered storage feature. This feature reduces your long-term storage costs without affecting log query, analysis, visualization, alerting, delivery, and processing capabilities. For more information, see Manage intelligent tiering.

Code example for creating a tiered storage LogStore

This example creates the SLSColdLogStore.go file. It then calls the CreateLogStore API to create a tiered data store. Example:

package main

import (
	"fmt"
	"os"
	"time"

	sls "github.com/aliyun/aliyun-log-go-sdk"
)

func main() {
	// The endpoint of Simple Log Service. This example uses the endpoint for Hangzhou. For other regions, use 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")
	// The temporary security token of the Resource Access Management (RAM) user role. This value is empty, indicating that no temporary security token is used. For more information, see authorized user.
	SecurityToken := ""
	// Create a Simple Log Service client.
	provider := sls.NewStaticCredentialsProvider(AccessKeyId, AccessKeySecret, SecurityToken)
	Client := sls.CreateNormalInterfaceV2(Endpoint, provider)
	// The project name.
	ProjectName := "aliyun-test-project"
	// The LogStore name.
	LogStoreName := "aliyun-test-logstore"

	err := Client.CreateLogStore(ProjectName, LogStoreName, 2, 2, true, 64)
	if err != nil {
		panic(err)
	}
	// Create the LogStore.
	logstore, err := Client.GetLogStore(ProjectName, LogStoreName)
	if err != nil {
		panic(err)
	}
	fmt.Println("create logstore successfully:", logstore.Name)
	// Update the data retention period in the hot tier 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)
	// Delete the LogStore.
	err = Client.DeleteLogStore(ProjectName, LogStoreName)
	if err != nil {
		panic(err)
	}
	fmt.Println("Delete Logstore successfully")
}

Call the UpdateLogStoreV2 API to update the LogStore and set tiered storage parameters. The following table describes the UpdateLogStoreV2 API parameters.

Parameter Name

Type

Required

Example

Description

ProjectName

String

Yes

aliyun-test-project

The project name.

When creating the client, you define the project_name. Therefore, you do not need to configure it here.

LogStoreName

String

Yes

aliyun-test-logstore

The LogStore name.

When creating the client, you define the logstore_name. Therefore, you do not need to configure it here.

TTL

Long

Yes

3000

The data retention period in the LogStore. Unit: days. Valid values: 1 to 3000. Data is deleted after this period.

Warning

After the log retention period reaches the set period, logs are deleted.

ShardCount

Long

Yes

2

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

HotTTL

String

Yes

60

The data retention period in the LogStore hot tier. The minimum value is 7 days. Unit: days. Valid values: 7 to 3000.

When the data storage period exceeds the configured data retention period for the hot tier, data moves to IA storage class (formerly cold storage).