You can call the ListVectors operation using Go SDK V2 to list all vectors in a specified vector index. This operation supports paging to traverse large amounts of vector data.
Permissions
By default, an Alibaba Cloud account has all permissions, while its associated RAM users and RAM roles have none. The Alibaba Cloud account or an administrator must grant them permissions by using a RAM policy or a bucket policy.
|
API |
Actions |
Description |
|
ListVectors |
|
Lists vector data. |
Method definition
func (c *VectorsClient) ListVectors(ctx context.Context, request *ListVectorsRequest, optFns ...func(*oss.Options)) (*ListVectorsResult, error)
Request parameters
|
Parameter |
Type |
Description |
|
params |
*ListVectorsRequest |
Specifies the request parameters, including the bucket name and index name. For more information, see ListVectorsRequest. |
|
optFns |
...func(*Options) |
(Optional) The operation-level configuration parameters. For more information, see Options. |
Return values
|
Type |
Description |
|
|
result |
*ListVectorsPaginator |
The paginator used to traverse the vector list. For more information, see ListVectorsPaginator. |
Example 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
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 the Alibaba Cloud account.")
flag.StringVar(&indexName, "index", "", "The name of the 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")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To use a public endpoint, set this to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.ListVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
ReturnMetadata: oss.Ptr(true),
ReturnData: oss.Ptr(false),
}
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)
}
for _, v := range page.Vectors {
log.Printf("vector:%v\n", v)
}
}
}
References
For the complete sample code on how to list vectors, see list_vectors.go.