This topic describes how to manage symbol links in a bucket with versioning enabled or suspended.
Create a symbol link
You can create a symbol link that directs to the current version of the target object.
Note In a bucket with versioning enabled or suspended, you cannot create a symbol link
for a delete marker.
A symbol link can has multiple versions that direct to different target objects. The version ID of the symbol link is automatically generated and is returned as the x-oss-version-id field in the response header.
You can run the following code to create a symbol link:
package main
import (
"fmt"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Creates an OSSClient instance.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
bucketName := "<yourBucketName>"
// Specifies the target object.
objectName := "<yourSymlinkName>"
// Specifies the symbol link that directs to the target object.
targetObjectName := "<yourSymlinkTargetName>"
// Obtains the bucket.
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Uploads the object to the target object.
err = bucket.PutObject(targetObjectName, strings.NewReader("target"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Creates a symbol link and obtains the version ID of the symbol link.
var retHeader http.Header
err = bucket.PutSymlink(objectName, targetObjectName, oss.GetResponseHeader(&retHeader))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Prints x-oss-version-id.
fmt.Println("x-oss-version-id:", oss.GetVersionId(retHeader))
}
For more information about creating a symbol link, see PutSymlink.
Obtain a symbol link
GetSymlink obtains the current version of the target symbol link by default. You can specify
the versionId in the request to obtain the specified version of a symbol link. If
the current version of the target symbol link is a delete marker, OSS returns the
404 Not Found error and includes
x-oss-delete-marker = true
and x-oss-version-id
in the response header.
Note You must have the read permission on a symbol link to obtain it.
You can run the following code to obtain a symbol link:
package main
import (
"fmt"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Creates an OSSClient instance.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
bucketName := "<yourBucketName>"
// Specifies the target object.
objectName := "<yourSymlinkName>"
// Obtains the bucket.
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtains the content of the object that the specified version of the symbol link directs to.
meta, err := bucket.GetSymlink(objectName,oss.VersionId("youObjectVersionId"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Prints the symbol link.
fmt.Println(meta.Get(oss.HTTPHeaderOssSymlinkTarget))
}
For more information about obtaining a symbol link, see GetSymlink.