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:PutVectorspermission granted to your Alibaba Cloud account, RAM user, or RAM role — via a RAM policy or a bucket policyThe
alibabacloud-oss-python-sdk-v2package installed
By default, an Alibaba Cloud account has all permissions. RAM users and RAM roles have no permissions until explicitly granted.
How it works
Create an
oss_vectors.Clientwith your region, account ID, and credentials.Build a list of vector records — each with a
key,data(float32 array), and optionalmetadata.Call
put_vectorswith the bucket name, index name, and vector list.
Permissions
| API | Action | Description |
|---|---|---|
| PutVectors | oss:PutVectors | Writes vector data to a vector index |
Method definition
put_vectors(request: PutVectorsRequest, **kwargs) → PutVectorsResultRequest parameters
| Parameter | Type | Description |
|---|---|---|
request | PutVectorsRequest | Includes the bucket name, index name, and vector records to write. See PutVectorsRequest. |
Return value
| Type | Description |
|---|---|
PutVectorsResult | The 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:
| Field | Required | Type | Description |
|---|---|---|---|
key | Yes | string | Unique record identifier within the index |
data | Yes | dict | Vector values as a float32 array: {"float32": [...]} |
metadata | No | dict | Key-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:
| Placeholder | Description | Example |
|---|---|---|
--region | The region where your bucket is located | cn-hangzhou |
--bucket | The name of your OSS bucket | my-bucket |
--index_name | The name of the vector index | my-index |
--account_id | Your Alibaba Cloud account ID | 123456789 |
--endpoint | (Optional) Custom endpoint for OSS access | oss-cn-hangzhou.aliyuncs.com |
References
For the complete sample code, see put_vectors.py.