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