Upload vectors (Go SDK V2)
Use the Go SDK V2 to call the PutVectors operation to upload vector data to a specified vector index.
Permissions
An Alibaba Cloud account has all permissions by default. However, Resource Access Management (RAM) users and RAM roles have no permissions by default. The Alibaba Cloud account owner or an administrator must grant operation permissions using a RAM policy or a bucket policy.
|
API |
Action |
Description |
|
PutVectors |
|
Writes vector data. |
Method definition
func (c *VectorsClient) PutVectors(ctx context.Context, request *PutVectorsRequest, optFns ...func(*oss.Options)) (*PutVectorsResult, error)
Request parameters
|
Parameter |
Type |
Description |
|
ctx |
context.Context |
The request context. |
|
request |
*PutVectorsRequest |
The request parameters, including the vector bucket name. For more information, see PutVectorsRequest. |
|
optFns |
...func(*Options) |
The optional configuration functions. |
Return values
|
Parameter |
Type |
Description |
|
result |
*PutVectorsResult |
The return value. This parameter is valid only when err is nil. For more information, see PutVectorsResult. |
|
err |
error |
The error message. This parameter is nil if the operation is successful. |
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(®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.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 || len(region) == 0 || len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To use public access, set this parameter to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.PutVectorsRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr("exampleIndex"),
Vectors: [ ]map[string]any{
{
"key": "vector1",
"data": map[string]any{
"float32": [ ]float32{1.2, 2.5, 3},
},
"metadata": map[string]any{
"Key1": "value2",
"Key2": [ ]string{"1", "2", "3"},
},
},
},
}
result, err := client.PutVectors(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put vectors %v", err)
}
log.Printf("put vectors result:%#v\n", result)
}
References
For the complete sample code for uploading vector data, see put_vectors.go.