All Products
Search
Document Center

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

Last Updated:Nov 26, 2025

This topic describes how to manage symbolic links in a versioning-enabled bucket.

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. Querying symbolic links requires the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user

Sample code

Create a symbolic link

Note
  • A symbolic link can have multiple versions and each link version can point to different objects. When you create a symbolic link in a versioned bucket, the version ID of the link is automatically generated by OSS and is returned as the x-oss-version-id header in the response.

  • In a versioned bucket, a symbolic link cannot be created for a delete marker.

Below is a code example for creating a symbolic link that points to the current version of an object.

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 // Region in which the bucket is located.
	bucketName string // Name of the bucket.
	objectName string // 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 symbolic link.
	var (
		symlinkName = "testsymlink" // Name of the symbolic link.
	)

	// 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 symbolic link 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 for symbolic link creation.
	putRequest := &oss.PutSymlinkRequest{
		Bucket: oss.Ptr(bucketName),  // Name of the bucket.
		Key:    oss.Ptr(symlinkName), // Name of the symbolic link.
		Target: oss.Ptr(objectName),  // Name of the target object.
	}

	// Execute the request.
	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

Note
  • To query a symbolic link, you must have read permissions on it.

  • By default, the GetSymlink operation queries the current version of a symbolic link. You can specify a version ID in the request to query the specified version of a symbolic link. If the current version of the symbolic link is a delete marker, OSS returns 404 Not Found and includes x-oss-delete-marker = true and x-oss-version-id in the response.

Below is a code example for querying 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 // Region in which the bucket is located.
	bucketName  string // Name of the bucket.
	symlinkName string // Name of the symbolic link.
)

// 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 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 symbolic link 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 query request for the symbolic link.
	getRequest := &oss.GetSymlinkRequest{
		Bucket:    oss.Ptr(bucketName),  // Name of the bucket.
		Key:       oss.Ptr(symlinkName), // Name of the symbolic link.
		VersionId: oss.Ptr("versionId"), // Specify the actual version ID.
	}

	// 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 name of the target object to which the symbolic link points.
	log.Printf("get symlink target object name:%#v\n", *getResult.Target)
}

References

  • For more information about the API operation for creating a symbolic link, see PutSymlink.

  • For more information about the API operation for querying a symbolic link, see GetSymlink.