📖 阿里云SLS API 使用指南

日志服务 Log Service - API Reference

📑 快速导航

1 🔑 认证与签名
阿里云SLS认证方式

使用HMAC-SHA1签名算法,基于AccessKeyId和AccessKeySecret进行身份验证。

签名算法:

SignString = Method + "\n" + 
             ContentMd5 + "\n" + 
             ContentType + "\n" + 
             Date + "\n" + 
             CanonicalizedHeaders + "\n" +
             CanonicalizedResource

Signature = Base64(HMAC-SHA1(SignString, AccessKeySecret))
Authorization = "LOG " + AccessKeyId + ":" + Signature
⚠️ 重要提示
  • Date必须使用RFC2616格式(如:Fri, 31 Oct 2025 08:00:00 GMT)
  • 自定义Header(x-log-*)必须按字典序排列
  • 查询参数必须按字典序排列
2 📦 获取项目列表
接口地址:GET https://{endpoint}/

Curl 示例:

curl --location 'https://cn-hangzhou.log.aliyuncs.com/' \
--header 'Date: Fri, 31 Oct 2025 08:00:00 GMT' \
--header 'Authorization: LOG {AccessKeyId}:{Signature}' \
--header 'x-log-apiversion: 0.6.0' \
--header 'x-log-signaturemethod: hmac-sha1' \
--header 'x-log-bodyrawsize: 0' \
--header 'Content-Length: 0'

响应示例:

{
  "count": 2,
  "total": 2,
  "projects": [
    {
      "projectName": "feishu-message",
      "status": "Normal",
      "owner": "1584650070640631",
      "description": "飞书聊天消息",
      "region": "cn-hangzhou",
      "createTime": "1760338014",
      "dataRedundancyType": "LRS"
    }
  ]
}
3 📁 获取LogStore列表
接口地址:GET https://{project}.{endpoint}/logstores

Curl 示例:

curl --location 'https://feishu-message.cn-hangzhou.log.aliyuncs.com/logstores' \
--header 'Date: Fri, 31 Oct 2025 08:00:00 GMT' \
--header 'Authorization: LOG {AccessKeyId}:{Signature}' \
--header 'x-log-apiversion: 0.6.0' \
--header 'x-log-signaturemethod: hmac-sha1' \
--header 'x-log-bodyrawsize: 0' \
--header 'Content-Length: 0'

响应示例:

{
  "count": 5,
  "total": 5,
  "logstores": [
    "feishu-message-data",
    "feishu-message-user",
    "feishu-message-groupname",
    "test"
  ]
}
4 📊 查询日志
接口地址:GET https://{project}.{endpoint}/logstores/{logstore}

Curl 示例:

curl --location 'https://feishu-message.cn-hangzhou.log.aliyuncs.com/logstores/feishu-message-data?type=log&from=1761897600&to=1761901200&query=*&reverse=true&line=50&offset=0' \
--header 'Date: Fri, 31 Oct 2025 08:00:00 GMT' \
--header 'Authorization: LOG {AccessKeyId}:{Signature}' \
--header 'x-log-apiversion: 0.6.0' \
--header 'x-log-signaturemethod: hmac-sha1' \
--header 'x-log-bodyrawsize: 0' \
--header 'Content-Length: 0'

查询参数:

参数 类型 说明
type string 固定值:log
from int 开始时间(Unix时间戳,秒)
to int 结束时间(Unix时间戳,秒)
query string 查询语句(* 表示所有日志)
reverse boolean 是否降序(true=降序,false=升序)
line int 返回的日志条数
offset int 偏移量(用于分页)
⚠️ 前提条件 LogStore必须配置索引才能查询日志。如果未配置,系统会自动提示创建索引。
5 ⚙️ 创建索引
接口地址:POST https://{project}.{endpoint}/logstores/{logstore}/index

Curl 示例:

curl --location 'https://feishu-message.cn-hangzhou.log.aliyuncs.com/logstores/test/index' \
--header 'Date: Fri, 31 Oct 2025 08:00:00 GMT' \
--header 'Authorization: LOG {AccessKeyId}:{Signature}' \
--header 'Content-Type: application/json' \
--header 'Content-MD5: {MD5_HASH}' \
--header 'x-log-apiversion: 0.6.0' \
--header 'x-log-signaturemethod: hmac-sha1' \
--header 'x-log-bodyrawsize: {BODY_LENGTH}' \
--data '{
  "line": {
    "token": [",", " ", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"],
    "caseSensitive": false
  },
  "ttl": 30
}'

请求Body参数:

参数 类型 说明
line.token array 分词符列表
line.caseSensitive boolean 是否大小写敏感
ttl int 索引数据保留天数
✅ 自动创建 系统已集成一键创建索引功能,当检测到LogStore无索引时,会自动提示创建。
6 ❓ 常见问题

Q: 如何计算签名?

A: 系统已自动处理签名计算,包括HMAC-SHA1加密、自定义Header排序、查询参数排序等。

Q: Content-MD5如何计算?

A: 对于POST请求,Content-MD5是请求体的MD5哈希值的大写十六进制字符串。

Q: 为什么查询日志报错IndexConfigNotExist?

A: LogStore未配置索引。系统会自动提示创建,点击"创建索引"按钮即可一键配置。

Q: 时间戳格式是什么?

A: Unix时间戳(秒级),如:1761897600 表示 2025-10-31 00:00:00

7 🔗 相关链接
8 🌍 可用区域列表
区域 Endpoint
华东1(杭州) cn-hangzhou.log.aliyuncs.com
华东2(上海) cn-shanghai.log.aliyuncs.com
华北2(北京) cn-beijing.log.aliyuncs.com
华南1(深圳) cn-shenzhen.log.aliyuncs.com
华南1(广州) cn-guangzhou.log.aliyuncs.com
中国香港 cn-hongkong.log.aliyuncs.com
新加坡 ap-southeast-1.log.aliyuncs.com
美国西部1(硅谷) us-west-1.log.aliyuncs.com