All Products
Search
Document Center

Object Storage Service:Manage symbolic links (Go SDK V2)

Last Updated:Aug 02, 2025

A symbolic link provides a convenient way to access frequently used files in a bucket, similar to a shortcut on Windows. This topic describes how to use the OSS SDK for Go to create and retrieve symbolic links.

Notes

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.

  • The examples in this topic show how to use access credentials that are read from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • To create a symbolic link, you must have the oss:PutObject permission. To retrieve a symbolic link, you must have 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

Parameter

Type

Description

ctx

context.Context

The context of the request. You can use this parameter to set the total time limit for the request.

request

*PutSymlinkRequest

The request parameters for the API operation to create a symbolic link. For more information, see PutSymlinkRequest.

*GetSymlinkRequest

The request parameters for the API operation to get a symbolic link. For more information, see GetSymlinkRequest.

optFns

...func(*Options)

(Optional) The operation-level configuration parameters. For more information, see Options.

Return values

Return value

Type

Description

result

*PutSymlinkResult

The return value of the API operation to create a symbolic link. This parameter is valid when err is nil. For more information, see RestoreObjectResult.

*GetSymlinkResult

The return value of the API operation to get a symbolic link. This parameter is valid when err is nil. For more information, see GetSymlinkResult.

err

error

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

Sample code

Create a symbolic link

You can use the following code to 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"
)

// Define global variables.
var (
	region     string // The region.
	bucketName string // The bucket name.
	objectName string // The object name.
)

// The init function is used to initialize command-line parameters.
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 object.")
}

func main() {
	// Parse command-line parameters.
	flag.Parse()

	// Define the name of the symbolic link.
	var (
		symlinkName = "testsymlink" // The name of the symbolic link.
	)

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the object name is empty.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Check whether the name of the symbolic link is empty.
	if len(symlinkName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, symlink name required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to set the symbolic link.
	putRequest := &oss.PutSymlinkRequest{
		Bucket: oss.Ptr(bucketName),  // The bucket name.
		Key:    oss.Ptr(symlinkName), // Specify the name of the symbolic link.
		Target: oss.Ptr(objectName),  // Specify the name of the object file to which the symbolic link points.
	}

	// Execute the request to set the symbolic link.
	putResult, err := client.PutSymlink(context.TODO(), putRequest)
	if err != nil {
		log.Fatalf("failed to put symlink %v", err)
	}

	// Print the result of setting the symbolic link.
	log.Printf("put symlink result:%#v\n", putResult)
}

Get a symbolic link

You can use the following code to retrieve a symbolic link and the name of the object file to which it points.

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

// Define global variables.
var (
	region      string // The region.
	bucketName  string // The bucket name.
	symlinkName string // The name of the symbolic link.
)

// The init function is used to initialize command-line parameters.
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() {
	// Parse command-line parameters.
	flag.Parse()

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the name of the symbolic link is empty.
	if len(symlinkName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, symlink name required")
	}

	// Load the default configurations and set the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

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

	// Execute the operation to get the symbolic link and process the result.
	getResult, err := client.GetSymlink(context.TODO(), getRequest)
	if err != nil {
		log.Fatalf("failed to get symlink %v", err)
	}

	// Print the name of the object file to which the symbolic link points.
	log.Printf("get symlink target object name:%#v\n", *getResult.Target)
}

References

  • For the complete sample code for symbolic links, see the GitHub examples put_symlink.go and get_symlink.go.

  • For more information about operations on symbolic links, see Symbolic links.

  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.

  • For more information about the API operation that you can call to retrieve a symbolic link, see GetSymlink.