All Products
Search
Document Center

:Manage symbolic links

Last Updated:Feb 27, 2025

A symbolic link provides quick access to an object in a bucket and facilitates access to a frequently used object. Symbolic links in Object Storage Service (OSS) work like file shortcuts on Windows and allow you to quickly access associated objects. This topic describes how to configure and query symbolic links using OSS SDK for Go.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by Object Storage Service (OSS), see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • Creating symbolic links requires the oss:PutObject permission, while querying symbolic links requires the oss:GetObject permission. For more information, see Grant custom policy to RAM users.

Methods

Configure symbolic links

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

Query symbolic links

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, which can be used to specify the total duration of the request.

request

*PutSymlinkRequest

The request parameters for configuring symbolic links. For details, see PutSymlinkRequest

*GetSymlinkRequest

The request parameters for querying symbolic links. For details, see GetSymlinkRequest

optFns

...func(*Options)

Optional. The operation-level parameter. For more information, see Options.

Return values

Value

Type

Description

result

*PutSymlinkResult

Return values for configuring symbolic links. This parameter is valid when the value of err is nil. For more information, see RestoreObjectResult.

*GetSymlinkResult

Return values for querying symbolic links. This parameter is valid when the value of err is nil. For more information, see GetSymlinkResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Sample code

Configure a symbolic link

The following code provides an example of how to configure 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"
)

// Specify the global variables.
var (
	region     string // The region in which the bucket is located.
	bucketName string // The name of the bucket.
	objectName string // The name of the object.
)

// Specify the init function 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()

	// Specify the name of the symlink.
	var (
		symlinkName = "testsymlink" // The name of the symlink.
	)

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

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

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

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

	// Load the default configurations and specify 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 configure the symlink.
	putRequest := &oss.PutSymlinkRequest{
		Bucket: oss.Ptr(bucketName),  // The name of the bucket
		Key:    oss.Ptr(symlinkName), // Specify the name of the symlink.
		Target: oss.Ptr(objectName),  // Specify the name of the object to which the symbolic link points.
	}

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

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

Query a symbolic link

The following code provides an example of how to query a symbolic link and the name of the object to which the symbolic link 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"
)

// Specify the global variables
var (
	region      string // The region in which the bucket is located.
	bucketName  string // The name of the bucket.
	symlinkName string // The name of the symlink.
)

// Specify the init function 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 specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

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

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

	// Load the default configurations and specify 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 query the symlink.
	getRequest := &oss.GetSymlinkRequest{
		Bucket: oss.Ptr(bucketName),  // The name of the bucket.
		Key:    oss.Ptr(symlinkName), // The name of the symlink.
	}

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

	// Display the result.
	log.Printf("get symlink target object name:%#v\n", *getResult.Target)
}

References

  • For the complete sample code that is used to manage symbolic links, visit put_symlink.go and get_symlink.go.

  • For more information about related operations, see Symbolic Links.

  • For more information about the API operation that you can call to configure symbolic links, visit PutSymlink.

  • For more information about the API operation that you can call to query symbolic links, visit GetSymlink.