Create a tiered storage logstore using Go SDK

更新时间:
复制 MD 格式

Create a logstore with tiered storage enabled by using the Simple Log Service Go SDK. This topic provides a code example for configuring hot and Infrequent Access (IA) storage tiers.

Prerequisites

Complete the following operations:

  • Install the protobuf dependency package by running the following command: go get -u github.com/gogo/protobuf/proto.

Background

The Manage intelligent tiered storage feature of Simple Log Service reduces long-term storage costs without affecting log query, analysis, visualization, alerting, shipping, or transformation.

Code example for creating a tiered storage logstore

The following example creates the SLSColdLogStore.go file and calls the CreateLogStore operation to create a tiered storage logstore:

package main

import (
	"fmt"
	"os"
	"time"

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

func main() {
	// Specify the Simple Log Service endpoint. 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)
	// Specify the project name.
	ProjectName := "aliyun-test-project"
	// Specify 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")
}

The following table describes the parameters of the UpdateLogStoreV2 operation for configuring tiered storage.

Parameter Name

Type

Required

Example

Description

ProjectName

String

Yes

aliyun-test-project

The project name.

This value is already defined when you create the client, so you do not need to configure it here.

LogStoreName

String

Yes

aliyun-test-logstore

The logstore name.

This value is already defined when you create the client, so you do not need to configure it here.

TTL

Long

Yes

3000

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

Warning

After the retention period expires, logs are deleted.

ShardCount

Long

Yes

2

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

HotTTL

String

Yes

60

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

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