Use the ListVectors operation in Go SDK V2 to retrieve all vectors stored in a vector index. The operation supports pagination to traverse large datasets across multiple requests.
Permissions
By default, an Alibaba Cloud account has full permissions. Resource Access Management (RAM) users and RAM roles have no permissions by default and must be granted access via a RAM policy or a bucket policy.
| API | Required action | Description |
|---|---|---|
ListVectors | oss:ListVectors | Lists vector data |
Method definition
func (c *VectorsClient) ListVectors(ctx context.Context, request *ListVectorsRequest, optFns ...func(*oss.Options)) (*ListVectorsResult, error)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
request | *ListVectorsRequest | Yes | Specifies the bucket name and index name. See ListVectorsRequest. |
optFns | ...func(*Options) | No | Operation-level configuration options. See Options. |
Return value
| Type | Description |
|---|---|
*ListVectorsResult | The result of the list operation. Use NewListVectorsPaginator to traverse pages. See ListVectorsPaginator. |
List all vectors using the paginator
To traverse all vectors in an index, use NewListVectorsPaginator. It wraps ListVectors and handles page tokens automatically. Call HasNext() to check for more pages and NextPage() to fetch each page.
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
indexName string
)
func init() {
flag.StringVar(®ion, "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 vector account.")
flag.StringVar(&indexName, "index", "", "The name of vector index.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accounId required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
// Load credentials from environment variables.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId)
client := vectors.NewVectorsClient(cfg)
// ReturnMetadata: true — include vector metadata in each page.
// ReturnData: false — omit raw vector data to reduce response size.
request := &vectors.ListVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
ReturnMetadata: oss.Ptr(true),
ReturnData: oss.Ptr(false),
}
// Create a paginator. Each call to NextPage() fetches one page of results.
p := client.NewListVectorsPaginator(request)
var i int
log.Println("Vectors:")
for p.HasNext() {
i++
page, err := p.NextPage(context.TODO())
if err != nil {
log.Fatalf("failed to get page %v, %v", i, err)
}
// page.Vectors contains the vectors returned in this page.
for _, v := range page.Vectors {
log.Printf("vector:%v\n", v)
}
}
}Run the example with the following flags:
go run main.go \
--region <region-id> \
--bucket <bucket-name> \
--account-id <account-id> \
--index <index-name>Replace the placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
<region-id> | Region where the vector bucket is located | cn-hangzhou |
<bucket-name> | Name of the vector bucket | my-vector-bucket |
<account-id> | Your Alibaba Cloud account ID | 123456789012 |
<index-name> | Name of the vector index to list | my-vector-index |
References
For the complete sample code, see list_vectors.go.
For the
ListVectorsPaginatorAPI, see ListVectorsPaginator.