All Products
Search
Document Center

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

Last Updated:Jun 03, 2026

Call the GetVectors operation using Go SDK V2 to retrieve vector data and its metadata based on specified keys.

Permissions

Alibaba Cloud accounts have all permissions by default. By default, Resource Access Management (RAM) users or RAM roles under an Alibaba Cloud account do not have any permissions. The Alibaba Cloud account or an administrator must grant permissions for this operation using a RAM policy or a bucket policy.

API

Action

Description

GetVectors

oss:GetVectors

Gets vector data.

Method definition

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

Request parameters

Parameter

Type

Description

ctx

context.Context

The request context.

request

*GetVectorsRequest

The request parameters, such as the bucket name, index name, and a list of vector keys. For more information, see GetVectorsRequest.

optFns

...func(*Options)

(Optional) API-level configuration parameters.

For more information, see Options.

Return values

Parameter

Type

Description

result

*GetVectorsResult

The return value. This parameter is valid only when err is nil. For more information, see GetVectorsResult.

err

error

The error message. If the operation is successful, this value is nil.

Sample code

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, account ID required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region).WithAccountId(accountId).
		// To access over the public network, set this to false or remove this line.
		WithUseInternalEndpoint(true)

	client := vectors.NewVectorsClient(cfg)

	request := &vectors.GetVectorsRequest{
		Bucket:         oss.Ptr(bucketName),
		IndexName:      oss.Ptr("index"),
		Keys:           []string{"key1", "key2", "key3"},
		ReturnData:     oss.Ptr(true),
		ReturnMetadata: oss.Ptr(false),
	}
	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)
}

References

For the complete sample code, see get_vectors.go.