All Products
Search
Document Center

Object Storage Service:Retrieve vector information (Go SDK V2)

Last Updated:Mar 20, 2026

Use the GetVectors operation to retrieve vector data and metadata by key from a vector index.

Prerequisites

Before you begin, ensure that you have:

Permissions

Alibaba Cloud accounts have all permissions by default. RAM users and RAM roles have no permissions by default. Grant the required permission using a RAM policy or a bucket policy.

APIActionDescription
GetVectorsoss:GetVectorsRetrieves vector data.

Method definition

func (c *VectorsClient) GetVectors(ctx context.Context, request *GetVectorsRequest, optFns ...func(*oss.Options)) (*GetVectorsResult, error)

Request parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
request*GetVectorsRequestThe request parameters, such as the bucket name, index name, and a list of vector keys. See GetVectorsRequest.
optFns...func(*Options)(Optional) API-level configuration parameters. See Options.

Return values

ParameterTypeDescription
result*GetVectorsResultThe return value. Valid only when err is nil. See GetVectorsResult.
errerrorThe error. nil if the operation succeeds.

Sample code

The following example retrieves vector data for three keys from a named index.

ReturnData and ReturnMetadata control which parts of the stored record are returned. Set ReturnData to false when you only need metadata — this skips the vector values and reduces response size.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)

var (
	region     string
	bucketName string
	accountId  string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the vector bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
	flag.StringVar(&accountId, "account-id", "", "The ID of the vector account.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	if len(accountId) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, accountId required")
	}

	// Load credentials from environment variables
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region).WithAccountId(accountId)

	client := vectors.NewVectorsClient(cfg)

	request := &vectors.GetVectorsRequest{
		Bucket:         oss.Ptr(bucketName),
		IndexName:      oss.Ptr("index"),
		Keys:           []string{"key1", "key2", "key3"},
		ReturnData:     oss.Ptr(true),    // Set to false to skip vector values and reduce response size
		ReturnMetadata: oss.Ptr(false),   // Set to true to include metadata in the response
	}
	result, err := client.GetVectors(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get vectors %v", err)
	}
	log.Printf("get vectors result:%#v\n", result)
}

Run the example with the required flags:

go run get_vectors.go \
  --region <region-id> \
  --bucket <bucket-name> \
  --account-id <account-id>

Replace the placeholders with your actual values:

PlaceholderDescriptionExample
<region-id>The region where the vector bucket is located.cn-hangzhou
<bucket-name>The name of the vector bucket.my-vector-bucket
<account-id>The ID of the vector account.123456789
Note: Credentials are loaded from environment variables by NewEnvironmentVariableCredentialsProvider(). Set your credentials as environment variables before running the example.

References

For the complete sample code, see get_vectors.go.