A symbolic link is an alias that points to an object in a bucket. Use symbolic links to:
Create shorter, readable names: Assign a concise name to an object with a long or complex key.
Maintain backward-compatible paths: Point old paths to new object locations after reorganizing a bucket.
Avoid duplicate storage: Have multiple logical paths point to the same object without copying its content.
This topic shows how to create and retrieve symbolic links using the OSS SDK for Go V2.
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) with a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see OSS regions and endpoints.Access credentials in the examples are read from environment variables. For configuration details, see Configure access credentials.
Creating a symbolic link requires the
oss:PutObjectpermission. Retrieving a symbolic link requires theoss:GetObjectpermission. For more information, see Grant custom access policies to RAM users.
Method definitions
Get a symbolic link
func (c *Client) GetSymlink(ctx context.Context, request *GetSymlinkRequest, optFns ...func(*Options)) (*GetSymlinkResult, error)Request parameters
| Parameter | Type | Description |
|---|---|---|
| ctx | context.Context | The request context. Use this to set a total time limit for the request. |
| request | *PutSymlinkRequest | Request parameters for creating a symbolic link. See PutSymlinkRequest. |
| *GetSymlinkRequest | Request parameters for retrieving a symbolic link. See GetSymlinkRequest. | |
| optFns | ...func(*Options) | (Optional) Operation-level configuration options. See Options. |
Return values
| Return value | Type | Description |
|---|---|---|
| result | *PutSymlinkResult | The result of creating a symbolic link. Valid when err is nil. See PutSymlinkResult. |
| *GetSymlinkResult | The result of retrieving a symbolic link. Valid when err is nil. See GetSymlinkResult. | |
| err | error | The request status. Non-nil if the request fails. |
Sample code
Create a symbolic link
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"
)
var (
region string
bucketName string
objectName string
)
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 target object.")
}
func main() {
flag.Parse()
// The name of the symbolic link to create.
symlinkName := "testsymlink"
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
if len(symlinkName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, symlink name required")
}
// Load default configuration and set credentials and region.
// Credentials are read from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
putRequest := &oss.PutSymlinkRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(symlinkName), // The name of the symbolic link.
Target: oss.Ptr(objectName), // The object that the symbolic link points to.
}
putResult, err := client.PutSymlink(context.TODO(), putRequest)
if err != nil {
log.Fatalf("failed to put symlink %v", err)
}
log.Printf("put symlink result:%#v\n", putResult)
}Get a symbolic link
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"
)
var (
region string
bucketName string
symlinkName string
)
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(&symlinkName, "symlink", "", "The name of the symlink.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(symlinkName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, symlink name required")
}
// Load default configuration and set credentials and region.
// Credentials are read from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
getRequest := &oss.GetSymlinkRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(symlinkName), // The name of the symbolic link.
}
getResult, err := client.GetSymlink(context.TODO(), getRequest)
if err != nil {
log.Fatalf("failed to get symlink %v", err)
}
// getResult.Target is a *string containing the key of the object the symbolic link points to.
log.Printf("symlink target object name: %#v\n", *getResult.Target)
}What's next
For complete sample code, see put_symlink.go and get_symlink.go on GitHub.
For an overview of symbolic links, including limits and access control behavior, see Symbolic links.
For the API reference, see PutSymlink and GetSymlink.