Manage symbolic links (Go SDK V1)

更新时间:
复制 MD 格式

Use the OSS Go SDK V1 to create and retrieve symbolic links in a versioning-enabled bucket.

Prerequisites

Before you begin, make sure that you have:

  • A versioning-enabled OSS bucket

  • The oss:PutObject permission to create symbolic links

  • The oss:GetObject permission to retrieve symbolic links

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid access credentials

For more information, see Attach a custom policy to a RAM user.

Usage notes

  • The examples use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For endpoint details, see Regions and endpoints.

  • The examples read credentials from environment variables. For configuration details, see Configure access credentials.

  • The examples create an OSSClient instance using an OSS endpoint. To use a custom domain or Security Token Service (STS), see Configure OSSClient instances.

Create a symbolic link

PutSymlink creates a symbolic link pointing to the current version of a target object. OSS automatically assigns a version ID to the new symlink and returns it in the x-oss-version-id response header.

Note

A symlink can have multiple versions. Each version can point to a different object. You cannot create a symlink for a delete marker in a versioning-enabled bucket.

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.
	// Endpoint: https://oss-cn-hangzhou.aliyuncs.com (China (Hangzhou) region public endpoint)
	// Region: cn-hangzhou
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	bucketName := "examplebucket"
	objectName := "exampledir/examplesymlink"         // Name of the symlink to create
	targetObjectName := "exampledir/exampleobject.txt" // Name of the target object

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

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

	// Create the symlink.
	// OSS returns the assigned version ID in the x-oss-version-id response header.
	var retHeader http.Header
	err = bucket.PutSymlink(objectName, targetObjectName, oss.GetResponseHeader(&retHeader))
	if err != nil {
		log.Fatalf("Failed to create symlink '%s' -> '%s': %v", objectName, targetObjectName, err)
	}

	// Print the version ID assigned to the new symlink.
	versionId := oss.GetVersionId(retHeader)
	log.Printf("x-oss-version-id: %s", versionId)
}

Get a symbolic link

GetSymlink retrieves the current version of a symbolic link by default. Pass oss.VersionId to retrieve a specific version.

If the current version of the symlink is a delete marker, OSS returns 404 Not Found with x-oss-delete-marker: true and the version ID in the response header.

Note

Read permissions on the symbolic link are required to call this operation.

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.
	// Endpoint: https://oss-cn-hangzhou.aliyuncs.com (China (Hangzhou) region public endpoint)
	// Region: cn-hangzhou
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	bucketName := "examplebucket"
	objectName := "exampledir/examplesymlink"                             // Name of the symlink to retrieve
	versionId := "CAEQMhiBgMDJgZCA0BYiIGY2Y2VkMTQyYjkzNDVjNTgzMTY2****" // Version ID of the symlink; omit to get the current version

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

	// Retrieve the specified version of the symlink.
	// The symlink target is stored in the x-oss-symlink-target response header,
	// accessible via the oss.HTTPHeaderOssSymlinkTarget constant.
	meta, err := bucket.GetSymlink(objectName, oss.VersionId(versionId))
	if err != nil {
		log.Fatalf("Failed to get symlink '%s' (version: %s): %v", objectName, versionId, err)
	}

	symlinkTarget := meta.Get(oss.HTTPHeaderOssSymlinkTarget)
	log.Printf("Symlink target: %s", symlinkTarget)
}

API reference