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

对象存储

复制全文
下载 pdf
向量
PutVectors(TOS Vectors Node.js SDK)
复制全文
下载 pdf
PutVectors(TOS Vectors Node.js SDK)
PutVectors 接口用于向量索引中批量插入向量,本文介绍如何通过 TOS Vectors Node.js SDK 的 PutVectors 接口完成该操作。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见配置长期访问凭证。​
  • TOS Vectors Node.js SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见常见配置场景。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 PutVectors 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:PutVectors​
代码示例​
向量索引中批量插入向量​
​
// 向量索引中批量插入向量​
​
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 = "test"​
​
/**​
* 处理 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() {​
// 请确保以下维度与创建索引时的 dimension 保持一致​
const dimension = 128;​
​
try {​
console.log(​
`Putting vectors into index: ${indexName} in vector bucket: ${vectorBucketName}...`,​
);​
​
// 1. 准备待写入的向量数据​
// 每个向量包含:​
// - key: 向量的唯一标识符​
// - data.float32: 与索引维度一致的浮点向量​
// - metadata: 可选的元数据,用于后续过滤或业务标注​
const vectors = [​
{​
key: `product-1-${Date.now()}`,​
data: {​
float32: Array.from({ length: dimension }, () => Math.random()),​
},​
metadata: {​
category: "book",​
price: 19.9,​
source: "demo",​
},​
},​
{​
key: `product-2-${Date.now()}`,​
data: {​
float32: Array.from({ length: dimension }, () => Math.random()),​
},​
metadata: {​
category: "electronics",​
price: 1999,​
source: "demo",​
},​
},​
];​
​
const putVectorsInput = {​
vectorBucketName,​
accountID: accountId,​
indexName,​
vectors,​
};​
​
// 2. 调用 putVectors 方法,将一批向量写入指定索引​
const result = await client.putVectors(putVectorsInput);​
​
// 3. 打印通用返回信息和业务数据​
console.log("PutVectors Request Succeeded:");​
console.log(" Request ID:", result.requestId);​
console.log(" Status Code:", result.statusCode);​
console.log(" Headers:", result.headers);​
​
// 当前 PutVectors 接口的业务返回体为空对象 {},主要通过 HTTP 状态码和请求 ID 判断是否成功​
console.log(" Business Data (PutVectors):", result.data);​
​
// 为方便排查问题,这里额外打印本次写入的向量 key 列表​
console.log(​
" Inserted Vector Keys:",​
vectors.map((v) => v.key),​
);​
} catch (error: any) {​
handleError(error);​
}​
}​
​
main();​
​
​
​
最近更新时间:2026.08.03 11:05:45
这个页面对您有帮助吗?
有用
有用
无用
无用