This topic describes how to use Simple Log Service SDK for Python 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.
ImportantThe 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 Python is installed. For more information, see Install Simple Log Service SDK for Python.
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.
Usage notes
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com
. If you want to access Simple Log Service by using other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com
. For more information about the supported regions and endpoints of Simple Log Service, see Endpoints.
Sample code
In this example, a file named SLSColdLogstore.py 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:
# encoding: utf-8
from __future__ import print_function
import time
import os
from aliyun.log import *
def 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'
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The name of the project.
project_name = 'aliyun-test-project'
# The name of the Logstore.
logstore_name= 'hot_ttl2'
# Create a Simple Log Service client.
client = LogClient(endpoint, access_key_id, access_key)
# Create a Logstore for which intelligent tiered storage is enabled. The Logstore can store logs for up to 3,000 days. Logs are automatically stored in the hot storage tier of the Logstore in the first 60 days.
client.create_logstore(project_name, logstore_name, ttl=3000, shard_count=3, hot_ttl=60)
# Display the period of time during which logs are stored in the hot storage tier of the Logstore.
resp = client.get_logstore(project_name, logstore_name)
print('hot ttl:', resp.get_hot_ttl())
# Change the period of time during which logs are stored in the hot storage tier of the Logstore to 61 days.
client.update_logstore(project_name, logstore_name, ttl=3000, shard_count=3, hot_ttl=61)
# Display the period of time during which logs are stored in the hot storage tier of the Logstore.
resp = client.get_logstore(project_name, logstore_name)
print('hot ttl:', resp.get_hot_ttl())
if __name__ == '__main__':
main()
The following table describes the parameters that you must configure when you call the CreateLogstore operation to create a Logstore for which intelligent tiered storage is enabled.
Parameter | Type | Required | Example | Description |
project_name | 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. |
logstore_name | String | Yes | hot_ttl2 | 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. Valid values: 1 to 3000. Unit: days. Warning After the retention period ends, data is automatically deleted. |
shard_count | Long | Yes | 2 | The number of shards. Valid values: 1 to 10. |
hot_ttl | String | Yes | 60 | The retention period of data in the hot storage tier of the Logstore. Valid values: 7 to 3000. Unit: days. Hot data that is stored for longer than the retention period specified by the hot_ttl 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. |