This topic describes how to use the Simple Log Service SDK for Python to create a Logstore with intelligent tiered storage enabled, retrieve tiered storage settings, and update the hot storage retention period.
How intelligent tiered storage works
Intelligent tiered storage reduces long-term log storage costs without affecting queries, analysis, visualization, alerting, data shipping, or data transformation.
A Logstore with intelligent tiered storage has two retention periods:
Total retention (
ttl): The total number of days that logs are stored in the Logstore. After this period, logs are automatically deleted.Hot storage retention (
hot_ttl): The number of days that logs remain in the hot storage tier. After this period, logs automatically move to the Infrequent Access storage tier (formerly known as the cold storage tier).
For example, if you set ttl=3000 and hot_ttl=60, logs from the last 60 days are stored in the hot storage tier for fast access, while older logs are stored in the Infrequent Access storage tier at a lower cost. All logs are deleted after 3,000 days.
To enable intelligent tiered storage in the console, see Enable the intelligent tiered storage feature.
Prerequisites
Before you begin, make sure that you have:
A Resource Access Management (RAM) user with the required permissions. See Create a RAM user and grant permissions to the RAM user
The
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables configured. See Configure environment variables in Linux, macOS, and WindowsSimple Log Service SDK for Python installed. See Install Simple Log Service SDK for Python
Use the AccessKey pair of a RAM user instead of your Alibaba Cloud account to call API operations. Do not save AccessKey credentials in your project code. Store them in environment variables to prevent leaks.
Usage notes
The following example uses the public endpoint for the China (Hangzhou) region: cn-hangzhou.log.aliyuncs.com. To access Simple Log Service from other Alibaba Cloud services in the same region, use the internal endpoint: cn-hangzhou-intranet.log.aliyuncs.com. For all supported regions and endpoints, see Endpoints.
Sample code
The following example creates a file named SLSColdLogstore.py that demonstrates how to create a Logstore with intelligent tiered storage, retrieve the hot storage retention period, and update it.
import os
from aliyun.log import LogClient
def main():
# Simple Log Service endpoint. Replace with your region's endpoint.
endpoint = 'cn-hangzhou.log.aliyuncs.com'
# Retrieve AccessKey credentials from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
project_name = 'aliyun-test-project'
logstore_name = 'hot_ttl2'
client = LogClient(endpoint, access_key_id, access_key_secret)
# --- Create a Logstore with intelligent tiered storage ---
# ttl: Total retention period (days). Valid values: 1 to 3000.
# hot_ttl: Hot storage retention period (days). Valid values: 7 to 3000.
# shard_count: Number of shards. Valid values: 1 to 10.
client.create_logstore(project_name, logstore_name, ttl=3000, shard_count=3, hot_ttl=60)
# --- Retrieve the hot storage retention period ---
resp = client.get_logstore(project_name, logstore_name)
print('hot ttl:', resp.get_hot_ttl())
# --- Update the hot storage retention period ---
client.update_logstore(project_name, logstore_name, ttl=3000, shard_count=3, hot_ttl=61)
# --- Verify the update ---
resp = client.get_logstore(project_name, logstore_name)
print('hot ttl:', resp.get_hot_ttl())
if __name__ == '__main__':
main()Expected output
hot ttl: 60
hot ttl: 61Parameters
The following table describes the parameters for create_logstore and update_logstore.
| Parameter | Type | Required | Example | Description |
|---|---|---|---|---|
project_name | String | Yes | aliyun-test-project | The name of the project. |
logstore_name | String | Yes | hot_ttl2 | The name of the Logstore. |
ttl | Long | Yes | 3000 | Total retention period of logs in the Logstore. Valid values: 1 to 3000. Unit: days. Logs are automatically deleted after this period. |
shard_count | Long | Yes | 3 | Number of shards. Valid values: 1 to 10. |
hot_ttl | Long | Yes | 60 | Hot storage retention period. Valid values: 7 to 3000. Unit: days. Logs older than this period automatically move to the Infrequent Access storage tier. The hot_ttl value must be less than or equal to the ttl value. |
After the total retention period (ttl) ends, logs are automatically deleted and cannot be recovered.