All Products
Search
Document Center

Object Storage Service:Manage symbolic links using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

A symbolic link is an alias that points to an object in a bucket. Use symbolic links to:

  • Create shorter, readable names: Assign a concise name to an object with a long or complex key.

  • Maintain backward-compatible paths: Point old paths to new object locations after reorganizing a bucket.

  • Avoid duplicate storage: Have multiple logical paths point to the same object without copying its content.

This topic shows how to create and retrieve symbolic links using the OSS SDK for Go V2.

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see OSS regions and endpoints.

  • Access credentials in the examples are read from environment variables. For configuration details, see Configure access credentials.

  • Creating a symbolic link requires the oss:PutObject permission. Retrieving a symbolic link requires the oss:GetObject permission. For more information, see Grant custom access policies to RAM users.

Method definitions

Create a symbolic link

func (c *Client) PutSymlink(ctx context.Context, request *PutSymlinkRequest, optFns ...func(*Options)) (*PutSymlinkResult, error)

Get a symbolic link

func (c *Client) GetSymlink(ctx context.Context, request *GetSymlinkRequest, optFns ...func(*Options)) (*GetSymlinkResult, error)

Request parameters

ParameterTypeDescription
ctxcontext.ContextThe request context. Use this to set a total time limit for the request.
request*PutSymlinkRequestRequest parameters for creating a symbolic link. See PutSymlinkRequest.
*GetSymlinkRequestRequest parameters for retrieving a symbolic link. See GetSymlinkRequest.
optFns...func(*Options)(Optional) Operation-level configuration options. See Options.

Return values

Return valueTypeDescription
result*PutSymlinkResultThe result of creating a symbolic link. Valid when err is nil. See PutSymlinkResult.
*GetSymlinkResultThe result of retrieving a symbolic link. Valid when err is nil. See GetSymlinkResult.
errerrorThe request status. Non-nil if the request fails.

Sample code

Create a symbolic link

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"
)

var (
	region     string
	bucketName string
	objectName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the target object.")
}

func main() {
	flag.Parse()

	// The name of the symbolic link to create.
	symlinkName := "testsymlink"

	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(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}
	if len(symlinkName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, symlink name required")
	}

	// Load default configuration and set credentials and region.
	// Credentials are read from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	putRequest := &oss.PutSymlinkRequest{
		Bucket: oss.Ptr(bucketName), // The bucket name.
		Key:    oss.Ptr(symlinkName), // The name of the symbolic link.
		Target: oss.Ptr(objectName),  // The object that the symbolic link points to.
	}

	putResult, err := client.PutSymlink(context.TODO(), putRequest)
	if err != nil {
		log.Fatalf("failed to put symlink %v", err)
	}

	log.Printf("put symlink result:%#v\n", putResult)
}

Get a symbolic link

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"
)

var (
	region      string
	bucketName  string
	symlinkName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&symlinkName, "symlink", "", "The name of the symlink.")
}

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(symlinkName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, symlink name required")
	}

	// Load default configuration and set credentials and region.
	// Credentials are read from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	getRequest := &oss.GetSymlinkRequest{
		Bucket: oss.Ptr(bucketName),  // The bucket name.
		Key:    oss.Ptr(symlinkName), // The name of the symbolic link.
	}

	getResult, err := client.GetSymlink(context.TODO(), getRequest)
	if err != nil {
		log.Fatalf("failed to get symlink %v", err)
	}

	// getResult.Target is a *string containing the key of the object the symbolic link points to.
	log.Printf("symlink target object name: %#v\n", *getResult.Target)
}

What's next