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