Note A symbolic link can have multiple versions and each link version can point to different objects. When you create a symbolic link in a versioned bucket, the version ID of the link is automatically generated by OSS and is returned as the x-oss-version-id header in the response.
In a versioned bucket, a symbolic link cannot be created for a delete marker.
Below is a code example for creating a symbolic link that points to the current version of an object.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // Region in which the bucket is located.
bucketName string // Name of the bucket.
objectName string // Name of the object.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Specify the name of the symbolic link.
var (
symlinkName = "testsymlink" // Name of the symbolic link.
)
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the name of the object is specified.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Check whether the name of the symbolic link is specified.
if len(symlinkName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, symlink name required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request for symbolic link creation.
putRequest := &oss.PutSymlinkRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
Key: oss.Ptr(symlinkName), // Name of the symbolic link.
Target: oss.Ptr(objectName), // Name of the target object.
}
// Execute the request.
putResult, err := client.PutSymlink(context.TODO(), putRequest)
if err != nil {
log.Fatalf("failed to put symlink %v", err)
}
// Display the result.
log.Printf("put symlink result:%#v\n", putResult)
}