All Products
Search
Document Center

Object Storage Service:Create a vector index (Go SDK V2)

Last Updated:Jun 03, 2026

Use the Go SDK V2 to call the PutVectorIndex operation to create a vector index in a vector bucket.

Permissions

An Alibaba Cloud account has all permissions by default. In contrast, a Resource Access Management (RAM) user or a RAM role does not have any permissions by default. The Alibaba Cloud account or an administrator must grant permissions for this operation using a RAM policy or a bucket policy.

API

Action

Description

PutVectorIndex

oss:PutVectorIndex

Creates a vector index.

Method definition

func (c *VectorsClient) PutVectorIndex(ctx context.Context, request *PutVectorIndexRequest, optFns ...func(*oss.Options)) (*PutVectorIndexResult, error) 

Request parameters

Parameter

Type

Description

ctx

context.Context

The request context.

request

*PutVectorIndexRequest

The request parameters. For more information, see PutVectorIndexRequest.

optFns

...func(*Options)

(Optional) The operation-level configuration parameters.

For more information, see Options.

Return values

Parameter

Type

Description

result

*PutVectorIndexResult

The return value of the operation. This parameter is valid only when err is nil. For more information, see PutVectorIndexResult.

err

error

The status of the request. If the request fails, err is not nil.

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
	indexName  string
)

func init() {
	flag.StringVar(&region, "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(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	if len(accountId) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, accountId required")
	}

	if len(indexName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, index required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region).WithAccountId(accountId).
		// To access the service over the public internet, set this parameter to false or remove this line.
		WithUseInternalEndpoint(true)

	client := vectors.NewVectorsClient(cfg)

	request := &vectors.PutVectorIndexRequest{
		Bucket:         oss.Ptr(bucketName),
		DataType:       oss.Ptr("float32"),
		Dimension:      oss.Ptr(128),
		DistanceMetric: oss.Ptr("cosine"),
		IndexName:      oss.Ptr(indexName),
		Metadata: map[string]any{
			"nonFilterableMetadataKeys": []string{"foo", "bar"},
		},
	}
	result, err := client.PutVectorIndex(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put vector index: %v", err)
	}
	log.Printf("put vector index result: %#v\n", result)
}

References

For the complete sample code for creating a vector index, see put_vector_index.go.