Symbolic links can be used to access objects that are frequently used in buckets. After you create a symbolic link for an object, you can use the symbolic link to access the object.

Create a symbolic link

The following code provides an example on how to create a symbolic link for an object:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
    "strings"
)


func main() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the name of the symbolic link. Example: examplesymlink. 
    symObjectKey := "examplesymlink.txt"
    // Specify the name of the object to which the symbolic link points. Example: exampleobject.txt. 
    objectName := "exampleobject.txt"

    bucket, err := client.Bucket(bucketName)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Create the symbolic link. 
    option := [] oss.Option{
        // Specify whether to overwrite the object with the same name when you create the symbolic link. 
        oss.ForbidOverWrite(true),
        // Specify the access control list (ACL) of the object. In this example, this parameter is set to private. 
        oss.ObjectACL(oss.ACLPrivate),
        // Specify the storage class of the object. In this example, this parameter is set to Standard. 
        oss.StorageClass(oss.StorageStandard),

    }
    err = bucket.PutSymlink(symObjectKey,objectName ,option...)

    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

}

Query the name of the object to which a symbolic link points

To query a symbolic link, you must have read permissions on the symbolic link. The following code provides an example on how to query the symbolic link and the name of the object to which the symbolic link points:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Create an OSSClient instance. 
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the endpoint based on your business requirements. 
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
    client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the name of the symbolic link. 
    symlinkName := "examplesymlink.txt"
   
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }

    // Query the name of the object to which the symbolic link points. 
    meta, err := bucket.GetSymlink(symlinkName)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println(meta.Get(oss.HTTPHeaderOssSymlinkTarget))
}            

References

  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.
  • For more information about the API operation that you can call to query a symbolic link, see GetSymlink.