All Products
Search
Document Center

Object Storage Service:Upload vectors (Python SDK V2)

Last Updated:Mar 20, 2026

Use put_vectors to write one or more vector records to a named vector index in your OSS bucket. Each record contains a unique key, a float32 vector, and optional metadata.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with a vector index created

  • The oss:PutVectors permission granted to your Alibaba Cloud account, RAM user, or RAM role — via a RAM policy or a bucket policy

  • The alibabacloud-oss-python-sdk-v2 package installed

By default, an Alibaba Cloud account has all permissions. RAM users and RAM roles have no permissions until explicitly granted.

How it works

  1. Create an oss_vectors.Client with your region, account ID, and credentials.

  2. Build a list of vector records — each with a key, data (float32 array), and optional metadata.

  3. Call put_vectors with the bucket name, index name, and vector list.

Permissions

APIActionDescription
PutVectorsoss:PutVectorsWrites vector data to a vector index

Method definition

put_vectors(request: PutVectorsRequest, **kwargs) → PutVectorsResult

Request parameters

ParameterTypeDescription
requestPutVectorsRequestIncludes the bucket name, index name, and vector records to write. See PutVectorsRequest.

Return value

TypeDescription
PutVectorsResultThe return value. For more information, see PutVectorsResult.

For the complete method definition, see put_vectors.

Vector record structure

Each vector record is a dict with the following fields:

FieldRequiredTypeDescription
keyYesstringUnique record identifier within the index
dataYesdictVector values as a float32 array: {"float32": [...]}
metadataNodictKey-value pairs for filtering or annotation

Example record:

{
    "key": "rec1",                          # Unique record identifier
    "data": {"float32": [0.1, 0.2, ...]},  # Vector values (float32 array)
    "metadata": {"field": "value"},         # Optional key-value metadata
}

Example

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

parser = argparse.ArgumentParser(description="vector put vectors 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()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Build the client 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)

    # Define vector records to upload.
    # Each record requires a unique key, float32 vector data, and optional metadata.
    vectors = [
        {
            "key": "key1",                                                          # Unique record identifier
            "data": {"float32": [0.1] * 128},                                       # 128-dimensional float32 vector
            "metadata": {"metadata1": "value1", "metadata2": "value2"}             # Optional key-value metadata
        },
        {
            "key": "key2",
            "data": {"float32": [0.2] * 128},
            "metadata": {"metadata3": "value3", "metadata4": "value4"}
        }
    ]

    # Upload all records in a single call
    result = vector_client.put_vectors(oss_vectors.models.PutVectorsRequest(
        bucket=args.bucket,
        index_name=args.index_name,
        vectors=vectors,
    ))

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

if __name__ == "__main__":
    main()

Replace the following placeholders before running:

PlaceholderDescriptionExample
--regionThe region where your bucket is locatedcn-hangzhou
--bucketThe name of your OSS bucketmy-bucket
--index_nameThe name of the vector indexmy-index
--account_idYour Alibaba Cloud account ID123456789
--endpoint(Optional) Custom endpoint for OSS accessoss-cn-hangzhou.aliyuncs.com

References

For the complete sample code, see put_vectors.py.