通过Go SDK V2调用PutVectors接口将向量数据上传到指定的向量索引中。
权限说明
阿里云账号默认拥有全部权限。阿里云账号下的RAM用户或RAM角色默认没有任何权限,需要阿里云账号或账号管理员通过RAM Policy或Bucket Policy授予操作权限。
API | Action | 说明 |
PutVectors |
| 写入向量数据。 |
方法定义
func (c *VectorsClient) PutVectors(ctx context.Context, request *PutVectorsRequest, optFns ...func(*oss.Options)) (*PutVectorsResult, error)
请求参数列表
参数名 | 类型 | 说明 |
ctx | context.Context | 请求上下文。 |
request | *PutVectorsRequest | 设置请求参数,包括向量存储桶名称等,具体请参见PutVectorsRequest |
optFns | ...func(*Options) | 可选的配置函数。 |
返回值列表
参数名 | 类型 | 说明 |
result | *PutVectorsResult | 返回值,当 err 为nil 时有效,具体请参见PutVectorsResult |
err | error | 错误信息,如果操作成功则为nil |
示例代码
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 vector 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)
}
相关文档
关于上传向量数据的完整示例代码,请参见put_vectors.go。
该文章对您有帮助吗?