API Key 管理
Table Store支援通過 API Key 認證方式訪問知識儲存服務和記憶儲存服務。使用 API Key 無需管理 AccessKey 金鑰組,支援獨立建立和吊銷,且強制 HTTPS 傳輸,適用於 AI 應用的快速整合情境。
認證方式對比
Table Store支援以下兩種認證方式。
認證方式 | 適用情境 | 支援的介面 |
AccessKey(AK/SK) | 全功能訪問 | 全部Table Store介面 |
API Key | AI 應用整合,無需管理金鑰組 | 僅知識儲存服務和記憶儲存服務的 API |
建立 API Key
在左側導覽列,單擊API Key。
在 API Key 管理頁面,單擊建立 API Key。
在建立 API Key 對話方塊,設定到期時間。
單擊建立。
請妥善保管密鑰值,僅在建立時顯示一次,無法再次查看。
使用 API Key
通過 SDK 或 curl 使用 API Key 訪問知識儲存服務和記憶儲存服務。API Key 支援兩項服務的全部 API 操作,包括建立、查詢、檢索和刪除。
通過 API Key 訪問時,必須使用 HTTPS 協議的訪問地址。
SDK
知識儲存服務和記憶儲存服務提供 Python SDK 和 TypeScript SDK。初始化用戶端時,使用 api_key 參數替代 AccessKey 憑證。
Python
安裝 SDK。
pip install tablestore-agent-storage以下樣本展示如何使用 API Key 建立知識庫並執行語義檢索。
from tablestore_agent_storage import AgentStorageClient
# 使用 API Key 初始化用戶端
client = AgentStorageClient(
api_key="your-api-key",
ots_endpoint="https://your-instance.cn-beijing.ots.aliyuncs.com",
ots_instance_name="your-instance-name",
)
# 建立知識庫
client.create_knowledge_base({"knowledgeBaseName": "product_docs"})
# 查看已建立的知識庫
response = client.list_knowledge_base()
for kb in response["data"]["knowledgeBases"]:
print(kb["knowledgeBaseName"])
# 檢索知識庫
results = client.retrieve({
"knowledgeBaseName": "product_docs",
"retrievalQuery": {"type": "TEXT", "text": "your query"}
})
for item in results["data"]["retrievalResults"]:
print(item["content"])TypeScript
安裝 SDK。
npm install @tablestore/agent-storage以下樣本展示如何使用 API Key 建立知識庫並執行語義檢索。
import { AgentStorageClient } from '@tablestore/agent-storage';
// 使用 API Key 初始化用戶端
const client = new AgentStorageClient({
apiKey: 'your-api-key',
endpoint: 'https://your-instance.cn-beijing.ots.aliyuncs.com',
instanceName: 'your-instance-name',
});
// 建立知識庫
await client.createKnowledgeBase({ knowledgeBaseName: 'product_docs' });
// 查看已建立的知識庫
const response = await client.listKnowledgeBase({});
for (const kb of response.data.knowledgeBases) {
console.log(kb.knowledgeBaseName);
}
// 檢索知識庫
const results = await client.retrieve({
knowledgeBaseName: 'product_docs',
retrievalQuery: { type: 'TEXT', text: '查詢文本' },
});
for (const item of results.data.retrievalResults) {
console.log(item.content);
}curl
通過 HTTP 要求直接存取時,在要求標頭中添加 x-ots-apikey 和 x-ots-instancename 欄位。
以下樣本展示如何通過 curl 建立知識庫。
curl -X POST 'https://your-instance.cn-beijing.ots.aliyuncs.com/CreateKnowledgeBase' \
-H 'x-ots-instancename: your-instance-name' \
-H 'x-ots-apikey: your-api-key' \
-H 'Content-Type: application/json' \
-d '{"knowledgeBaseName": "product_docs"}'以下樣本展示如何通過 curl 查看已建立的知識庫列表。
curl -X POST 'https://your-instance.cn-beijing.ots.aliyuncs.com/ListKnowledgeBase' \
-H 'x-ots-instancename: your-instance-name' \
-H 'x-ots-apikey: your-api-key' \
-H 'Content-Type: application/json' \
-d '{}'使用限制
API Key 僅支援訪問知識儲存服務和記憶儲存服務的 API。如需訪問Table Store的其他 API,請使用 AccessKey 認證。
建立 API Key 時,系統會自動建立一個專屬 RAM 使用者並綁定
AliyunOTSFullAccess權限原則。API Key 的許可權等同於該 RAM 使用者的許可權,如需調整請前往 RAM 控制台修改對應使用者的權限原則。通過 API Key 訪問知識儲存服務和記憶儲存服務時,系統同時校正 API 操作許可權和
ots:CallWithBearerToken許可權。如果對應 RAM 使用者缺少其中任一許可權,請求將被拒絕。