全部產品
Search
文件中心

Object Storage Service:建立向量 Index(Python SDK V2)

更新時間:Sep 24, 2025

使用Python SDK V2調用PutVectorIndex介面在向量Bucket中建立向量索引。

許可權說明

阿里雲帳號預設擁有全部許可權。阿里雲帳號下的RAM使用者或RAM角色預設沒有任何許可權,需要阿里雲帳號或帳號管理員通過RAM PolicyBucket Policy授予操作許可權。

API

Action

說明

PutVectorIndex

oss:PutVectorIndex

建立向量索引。

方法定義

put_vector_index(request: PutVectorIndexRequest, **kwargs) → PutVectorIndexResult

請求參數列表

參數名

類型

說明

request

PutVectorIndexRequest

佈建要求參數,例如設定向量儲存桶的名稱等,具體請參見PutVectorIndexRequest

傳回值列表

類型

說明

PutVectorIndexResult

傳回值,具體請參見PutVectorIndexResult

關於建立向量Index方法的完整定義,請參見put_vector_Index

範例程式碼

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

parser = argparse.ArgumentParser(description="vector put vector index sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
parser.add_argument('--account_id', help='The account id.', 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

    vector_client = oss_vectors.Client(cfg)

    result = vector_client.put_vector_index(oss_vectors.models.PutVectorIndexRequest(
        bucket=args.bucket,
        index_name=args.index_name,
        dimension=512,
        data_type='float32',
        distance_metric='euclidean',
        metadata={"nonFilterableMetadataKeys": ["key1", "key2"]}
    ))

    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
    )

if __name__ == "__main__":
    main()

相關文檔

關於建立向量Bucket的完整範例程式碼,請參見put_vector_index.py