列举Vectors(Python SDK V2)

通过Python SDK V2调用ListVectors接口列出指定向量索引中的所有向量,支持分页遍历大量向量数据。

权限说明

阿里云账号默认拥有全部权限。阿里云账号下的RAM用户或RAM角色默认没有任何权限,需要阿里云账号或账号管理员通过RAM PolicyBucket Policy授予操作权限。

API

Action

说明

ListVectors

oss:ListVectors

列举向量数据。

方法定义

Python SDK V2提供了两种方式列举向量数据:

  • list_vectors():直接调用接口,需要手动处理分页。

  • list_vectors_paginator():使用分页器,SDK会自动处理分页逻辑,推荐使用。

list_vectors(request: ListVectorsRequest, **kwargs) → ListVectorsResult[source]

请求参数列表

参数名

类型

说明

request

ListVectorsRequest

设置请求参数,具体请参见ListVectorsRequest

返回值列表

类型

说明

ListVectorsResult

返回值,具体请参见ListVectorsResult

关于列举向量方法的完整定义,请参见list_vectors

# 使用分页器
list_vectors_paginator(**kwargs) → ListVectorsPaginator[source]

返回值列表

类型

说明

ListVectorsPaginator

返回值,具体请参见ListVectorsPaginator

关于使用分页器列举向量方法的完整定义,请参见list_vectors_paginator

示例代码

import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors

parser = argparse.ArgumentParser(description="list vectors sample")

parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--account_id', help='The account id.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--index_name', help='The name of the vector index.', required=True)

def main():

    args = parser.parse_args()

    # Loading credentials values from the environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Using the SDK's default configuration
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    cfg.account_id = args.account_id
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss_vectors.Client(cfg)

    # Create the Paginator for the ListVectors operation
    paginator = client.list_vectors_paginator()

    # Create request with bucket and index name
    request = oss_vectors.models.ListVectorsRequest(
        bucket=args.bucket,
        index_name=args.index_name
    )

    # Iterate through the vectors pages
    for page in paginator.iter_page(request):
        for o in page.vectors:
            print(f'Vector: {o}')

if __name__ == "__main__":
    main()

相关文档

关于列出向量的完整示例代码,请参见list_vectors.py