All Products
Search
Document Center

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

Last Updated:Nov 28, 2025

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

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.

  • To create a symbolic link, you must have the oss:PutObject permission. To query a symbolic link, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

Create a symbolic link

You can create a symbolic link that points to the current version of an object file.

Note

You cannot create a symbolic link for a delete marker in a versioning-enabled bucket.

A symbolic link can have multiple versions. Each version can point to a different object file. The version ID is automatically generated by OSS and returned in the x-oss-version-id response header.

The following code shows how to create a symbolic link:

package main

import (
	"log"
	"net/http"
	"strings"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the bucket name.
	bucketName := "yourBucketName"
	// Specify the name of the symbolic link.
	objectName := "yourSymlinkName"
	// Specify the name of the object file.
	targetObjectName := "yourObjectName"

	// Create a bucket instance.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Upload a file to the object file.
	err = bucket.PutObject(targetObjectName, strings.NewReader("target"))
	if err != nil {
		log.Fatalf("Failed to put object '%s': %v", targetObjectName, err)
	}

	// Create a symbolic link and obtain its version information.
	var retHeader http.Header
	err = bucket.PutSymlink(objectName, targetObjectName, oss.GetResponseHeader(&retHeader))
	if err != nil {
		log.Fatalf("Failed to create symlink '%s' to '%s': %v", objectName, targetObjectName, err)
	}

	// Print x-oss-version-id.
	versionId := oss.GetVersionId(retHeader)
	log.Printf("x-oss-version-id: %s", versionId)
}

Get a symbolic link

By default, the GetSymlink operation retrieves the current version of a symbolic link. You can specify a version ID to retrieve a specific version. 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 the version ID x-oss-version-id in the response header.

Note

To retrieve a symbolic link, you must have read permissions on the symbolic link.

The following code shows how to retrieve a symbolic link:

package main

import (
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the bucket name.
	bucketName := "yourBucketName"
	// Specify the name of the symbolic link.
	objectName := "yourSymlinkName"
	// Specify the version ID of the symbolic link.
	versionId := "yourObjectVersionId"

	// Create a bucket instance.
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Get the specified version of the symbolic link.
	meta, err := bucket.GetSymlink(objectName, oss.VersionId(versionId))
	if err != nil {
		log.Fatalf("Failed to get symlink '%s' with version ID '%s': %v", objectName, versionId, err)
	}

	// Print the target of the symbolic link.
	symlinkTarget := meta.Get(oss.HTTPHeaderOssSymlinkTarget)
	log.Printf("Symlink Target: %s", symlinkTarget)
}

References

  • For more information about the API operation used to create a symbolic link, see PutSymlink.

  • For more information about the API operation used to retrieve a symbolic link, see GetSymlink.