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

对象存储

复制全文
下载 pdf
向量
DeleteVectors(TOS Vectors Node.js SDK)
复制全文
下载 pdf
DeleteVectors(TOS Vectors Node.js SDK)
DeleteVectors 接口用于批量删除向量,本文介绍如何通过 TOS Vectors Node.js SDK 的 DeleteVectors 接口完成该操作。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见配置长期访问凭证。​
  • TOS Vectors Node.js SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见常见配置场景。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用 IAM 用户执行 DeleteVectors 操作,主账号需通过 IAM策略和桶策略为该 IAM 用户授予以下权限:tosvectors:DeleteVectors​
代码示例​
批量删除向量​
​
// 批量删除向量​
​
import {​
TosVectorsClient,​
TosClientError,​
TosServerError,​
} from "@volcengine/tos-sdk";​
​
// 从环境变量中获取认证信息和配置​
const client = new TosVectorsClient({​
accessKeyId: process.env["TOS_ACCESS_KEY"] as string, // TOS_ACCESS_KEY: 您的 Access Key ID​
accessKeySecret: process.env["TOS_SECRET_KEY"] as string, // TOS_SECRET_KEY: 您的 Secret Key​
region: 'Provide your region',​
endpoint: 'Provide your vector endpoint',​
});​
​
// 从环境变量中读取向量桶名称和账号 ID​
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() {​
// 要删除的向量 key 列表,示例使用三个占位 key,实际使用时请替换为业务侧真实向量 key​
const keys = ["key1", "key2", "key3"];​
​
try {​
console.log(​
`开始批量删除向量,向量桶: ${vectorBucketName},索引: ${indexName},待删除 key 数量: ${keys.length}...`,​
);​
​
// 1. 调用 deleteVectors 方法,批量删除指定 key 对应的向量​
const result = await client.deleteVectors({​
vectorBucketName,​
accountID: accountId,​
indexName,​
keys,​
});​
​
// 2. 打印通用返回信息​
console.log("Request Succeeded:");​
console.log(" Request ID:", result.requestId);​
console.log(" Status Code:", result.statusCode);​
console.log(" Headers:", result.headers);​
​
// 3. DeleteVectorsOutput 当前为空对象,但仍打印 Result Data,便于调试以及后续接口扩展时复用​
console.log(" Result Data:", result.data);​
} catch (error: any) {​
handleError(error);​
}​
}​
​
main();​
​
​
​
最近更新时间:2026.08.03 11:05:44
这个页面对您有帮助吗?
有用
有用
无用
无用