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

对象存储

复制全文
下载 pdf
向量索引
GetIndex(TOS Vectors Python SDK)
复制全文
下载 pdf
GetIndex(TOS Vectors Python SDK)
get_index 接口用于查询指定向量索引的元数据与状态,本文介绍如何通过 TOS Vectors Python SDK 的 get_index 接口获取索引配置、维度、距离类型及创建时间。​
注意事项​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见 配置长期访问凭证。​
  • TOS Vectors Python SDK 支持使用临时访问凭证;也支持配置超时机制、重试策略等多种机制,您可以根据业务需求灵活设置,提升应用安全性,详细介绍,请参见 常见配置场景。​
权限说明​
主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用子账号执行 get_index 操作,主账号需通过 IAM策略和桶策略为该子账号授予以下权限:​
tosvectors:GetIndex​
代码示例​
获取指定向量索引的信息​
​
# 获取指定向量索引的信息​
import os​
import tos​
​
# 从环境变量获取 AK 和 SK 信息。​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 https://tosvectors-cn-beijing.volces.com,your region 填写 cn-beijing。​
endpoint = 'Provide your endpoint'​
region = 'Provide your region'​
account_id = os.getenv('TOS_ACCOUNT_ID')​
vector_bucket_name = 'Provide your bucket name'​
​
try:​
# 创建 VectorClient 对象,对向量的操作都通过 VectorClient 实现​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# 指定要获取的向量索引名称​
index_name = 'examplevectorindex'​
​
# 获取向量索引信息​
result = client.get_index(​
vector_bucket_name=vector_bucket_name,​
account_id=account_id,​
index_name=index_name​
)​
​
# 打印请求结果​
print('获取向量索引信息成功')​
print('Request ID:', result.request_id)​
print('Status Code:', result.status_code)​
​
# 打印索引详细信息​
if result.index:​
print('\n索引详细信息:')​
print('索引名称:', result.index.index_name)​
print('数据类型:', result.index.data_type)​
print('向量维度:', result.index.dimension)​
print('距离度量方式:', result.index.distance_metric)​
print('创建时间:', result.index.creation_time)​
print('索引TRN:', result.index.index_trn)​
​
# 打印元数据配置信息​
if result.index.metadata_configuration:​
print('非可过滤元数据键:', result.index.metadata_configuration.non_filterable_metadata_keys)​
​
except tos.exceptions.TosClientError as e:​
# 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息​
print('fail with server error, code: {}'.format(e.code))​
# request id 可定位具体问题,强烈建议日志中保存​
print('error with request id: {}'.format(e.request_id))​
print('error with message: {}'.format(e.message))​
print('error with http code: {}'.format(e.status_code))​
print('error with ec: {}'.format(e.ec))​
print('error with request url: {}'.format(e.request_url))​
except Exception as e:​
print('fail with unknown error: {}'.format(e))​
​
相关文档​
关于 GetIndex 的 API 文档,请参见 GetIndex。​
​
最近更新时间:2026.07.30 18:48:44
这个页面对您有帮助吗?
有用
有用
无用
无用