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. RAM users and RAM roles have no permissions by default. Grant the required permission using a RAM policy or a bucket policy.
| API | Action | Description |
|---|---|---|
| PutVectors | oss:PutVectors | Writes vector data. |
Method definition
func (c *VectorsClient) PutVectors(ctx context.Context, request *PutVectorsRequest, optFns ...func(*oss.Options)) (*PutVectorsResult, error)Parameters
| Parameter | Type | Description |
|---|---|---|
| ctx | context.Context | The request context. |
| request | *PutVectorsRequest | The request parameters, including the vector bucket name. See PutVectorsRequest. |
| optFns | ...func(*Options) | Optional configuration functions. |
Return values
| Parameter | Type | Description |
|---|---|---|
| result | *PutVectorsResult | The return value. Valid only when err is nil. See PutVectorsResult. |
| err | error | The error. nil if the operation succeeds. |
Sample code
The following example uploads one vector entry with float32 values and metadata to a vector index named exampleIndex. Credentials are loaded from environment variables.
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)
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)
}For the complete sample, see put_vectors.go.