You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Vector
GetVectors(TOS Vectors Python SDK)
Copy page
Download PDF
GetVectors(TOS Vectors Python SDK)
The get_vectors interface retrieves vector data in batches. This article explains how to use the get_vectors interface of the TOS Vectors Python SDK to read vectors corresponding to a specified list of IDs.​
Notes​
  • To prevent key leakage, the sample code reads the Access Key ID and Secret Access Key from environment variables. Before using the SDK, configure the long-term access credentials. For details, see configure long-term access credentials.​
  • The TOS Vectors Python SDK supports temporary access credentials and allows flexible settings for timeout mechanisms, retry policies, and more to enhance application security based on business needs. For details, see common configuration scenarios.​
  • TOS Vectors supports float32 precision for vectors. Vectors with higher precision may cause precision loss.​
Permissions required​
The main account has all permissions by default, while IAM users have no permissions by default. If you need to use a sub-account to perform the get_vectors operation, the main account must grant the following permissions to the sub-account through IAM policy and bucket policy:​
tosvectors:GetVectors​
Code example​
Batch query vectors by vector key​
​
# Batch query vectors based on vector keys.​
import os​
import tos​
​
# Retrieve AK and SK information from environment variables.​
ak = os.getenv('TOS_ACCESS_KEY')​
sk = os.getenv('TOS_SECRET_KEY')​
# Fill in your endpoint and your region with the endpoint corresponding to the bucket's region. # For example, for China (Beijing), set your endpoint to https://tosvectors-cn-beijing.volces.com and your region to cn-beijing.​
endpoint = 'Provide your endpoint'​
region = 'Provide your region'​
vector_bucket_name = 'Provide your bucket name'​
account_id = os.getenv('TOS_ACCOUNT_ID')​
​
try:​
# Create a VectorClient object; all vector operations are handled through VectorClient.​
client = tos.VectorClient(ak, sk, endpoint, region)​
​
# Set query parameters.​
index_name = "test" # Vector index name​
keys = ["product-vector-0", "product-vector-1", "product-vector-2"] # List of vector keys to query​
​
# Call the get_vectors API to query vectors in batches (without returning data and metadata).​
result = client.get_vectors(​
vector_bucket_name=vector_bucket_name,​
account_id=account_id,​
index_name=index_name,​
keys=keys,​
return_data=True, # Return vector data​
return_metadata=True # Return vector metadata​
)​
​
# Print query results.​
print(f"Query successfully, status code: {result.status_code}")​
print(f"Number of returned vectors: {len(result.vectors)}")​
​
# Iterate through the returned vector results.​
for vector in result.vectors:​
print(f"Vector key: {vector.key}")​
print(f"Vector data: {vector.data.float32}")​
print(f"Vector metadata: {vector.metadata}")​
print("---")​
​
except tos.exceptions.TosClientError as e:​
# The operation failed and a client exception was caught. This is usually due to invalid request parameters or a network exception.​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# The operation failed and a server exception occurred. See the response for the error details.​
print('fail with server error, code: {}'.format(e.code))​
# Use the request ID for troubleshooting purposes. We strongly recommend including the request ID in the logs.​
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))​
​
Related documentation​
For the API documentation on GetVectors, see GetVectors.​
Last updated: 2026.02.06 18:01:46