You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
向量索引
GetIndex(TOS Vectors Node.js SDK)
复制全文
下载 pdf
GetIndex(TOS Vectors Node.js SDK)
GetIndex 接口用于获取指定向量索引的信息,本文介绍如何通过 TOS Vectors Node.js SDK 的 GetIndex 接口完成该操作。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见配置长期访问凭证。​
  • TOS Vectors Node.js SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见常见配置场景。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 GetIndex 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:GetIndex​
代码示例​
获取指定向量索引的信息​
​
// 获取指定向量索引的信息​
​
import {​
TosVectorsClient,​
TosClientError,​
TosServerError,​
} from "@volcengine/tos-sdk";​
​
// 从环境变量中获取认证信息和配置​
const client = new TosVectorsClient({​
accessKeyId: process.env["TOS_ACCESS_KEY"], // TOS_ACCESS_KEY: 您的 Access Key ID​
accessKeySecret: process.env["TOS_SECRET_KEY"], // TOS_SECRET_KEY: 您的 Secret Key​
region: 'Provide your region',​
endpoint: 'Provide your vector endpoint',​
});​
​
const vectorBucketName = 'Provide your vector bucket name';​
const accountId = process.env["TOS_ACCOUNT_ID"];​
const indexName = "example-index-1769428012143";​
​
/**​
* 处理 SDK 调用过程中可能出现的错误。​
* @param error 捕获到的错误对象​
*/​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
// 客户端异常,通常是参数错误或网络问题​
console.log("Client Error:");​
console.log(" Message:", error.message);​
if (error.stack) {​
console.log(" Stack:", error.stack);​
}​
} else if (error instanceof TosServerError) {​
// 服务端异常,表示请求已送达但处理失败​
console.log("Server Error:");​
console.log(" Request ID:", error.requestId);​
console.log(" Status Code:", error.statusCode);​
console.log(" Code:", error.code);​
console.log(" Message:", error.message);​
console.log(" Headers:", error.headers);​
} else {​
// 其他未知异常​
console.log("Unexpected Error:");​
console.log(" Message:", error.message);​
}​
}​
​
async function main() {​
try {​
console.log(​
`Getting information for index: ${indexName} in vector bucket: ${vectorBucketName}...`,​
);​
​
// 1. 调用 getIndex 方法,获取指定向量索引的详细信息​
const result = await client.getIndex({​
vectorBucketName,​
accountID: accountId,​
indexName,​
});​
​
// 2. 打印通用返回信息​
console.log("Request Succeeded:");​
console.log(" Request ID:", result.requestId);​
console.log(" Status Code:", result.statusCode);​
console.log(" Headers:", result.headers);​
​
// 3. 打印索引的核心配置字段​
const index = result.data.index;​
console.log(" Index Info:");​
console.log(` - Index Name: ${index.indexName}`);​
console.log(` - Index TRN: ${index.indexTrn}`);​
console.log(` - Creation Time: ${index.creationTime}`);​
console.log(` - Data Type: ${index.dataType}`);​
console.log(` - Dimension: ${index.dimension}`);​
console.log(` - Distance Metric: ${index.distanceMetric}`);​
console.log(` - Vector Bucket Name: ${index.vectorBucketName}`);​
console.log(​
" - Non-filterable Metadata Keys:",​
index.metadataConfiguration.nonFilterableMetadataKeys,​
);​
} catch (error: any) {​
handleError(error);​
}​
}​
​
main();​
​
​
​
最近更新时间:2026.08.03 11:05:45
这个页面对您有帮助吗?
有用
有用
无用
无用