Symbolic links (symlinks) provide shortcuts to frequently used objects in an OSS on CloudBox bucket. After you create a symlink by calling the PutSymlink operation, you can use it to quickly access the target object.
Usage notes
PutSymlink does not validate whether the destination object exists, whether its storage class is valid, or whether you have access to it.
The ACL of the symlink and the destination object are checked only when operations such as GetObject access the destination object.
Request headers prefixed with x-oss-meta- are stored as user metadata of the symlink. Example: x-oss-meta-location. You can specify multiple metadata headers, but total metadata size cannot exceed 8 KB.
By default, if an object with the same name exists and you have access permissions, the new object overwrites the existing one. OSS returns 200 OK.
Procedure
Use an Alibaba Cloud SDK
You can create symbolic links by using the OSS SDK for Java, Python, or Go (Java SDK 3.15.0 or later, Python SDK V2 1.1.1 or later, or Go SDK V2 v1.2.1 or later). For OSS on CloudBox, the Python and Go SDKs are called in the same way as they are in public cloud OSS. For information about how to create a client, see SDK support. The following code examples use the Java, Python, and Go SDKs.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the data endpoint for the OSS on CloudBox bucket.
String endpoint = "https://cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the OSS on CloudBox bucket. For example, examplebucket.
String bucketName = "examplebucket";
// Specify the region where the OSS on CloudBox bucket is located.
String region = "cn-hangzhou";
// Specify the CloudBox ID.
String cloudBoxId = "cb-f8z7yvzgwfkl9q0h****";
// Specify the name for the symbolic link.
String symLink = "yourSymLink";
// Specify the name of the object file that the symbolic link points to.
String destinationObjectName = "yourDestinationObjectName";
// Create an OSSClient instance.
// When the OSSClient instance is no longer needed, call the shutdown method to release its resources.
ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
conf.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(new DefaultCredentialProvider(credentialsProvider.getCredentials()))
.clientConfiguration(conf)
.region(region)
.cloudBoxId(cloudBoxId)
.build();
try {
// Set metadata for the symbolic link.
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setContentType("text/plain");
// Set the value of the custom metadata property to property-value.
// metadata.addUserMetadata("property", "property-value");
// Specify whether to overwrite an object with the same name when you create the symbolic link.
// metadata.setHeader("x-oss-forbid-overwrite", "true");
// Specify the access permissions for the object.
// metadata.setHeader(OSSHeaders.OSS_OBJECT_ACL, CannedAccessControlList.Default);
// Specify the storage class for the object.
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
// Create a CreateSymlinkRequest object.
CreateSymlinkRequest createSymlinkRequest = new CreateSymlinkRequest(bucketName, symLink, destinationObjectName);
// Set the metadata.
// createSymlinkRequest.setMetadata(metadata);
// Create the symbolic link.
ossClient.createSymlink(createSymlinkRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
import alibabacloud_oss_v2 as oss
# Obtain access credentials from environment variables. Before you run this code example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the CloudBox is located, such as cn-hangzhou.
cfg.region = "cn-hangzhou"
# Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
cfg.cloud_box_id = "cb-f8z7yvzgwfkl9q0h****"
# Specify the data domain in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
cfg.endpoint = "cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com"
# Create an OSS client.
client = oss.Client(cfg)
# Create a symbolic link. key specifies the name of the symbolic link, and target specifies the destination object to which the symbolic link points.
result = client.put_symlink(oss.PutSymlinkRequest(
bucket="examplebucket",
key="yourSymLink",
target="yourDestinationObjectName",
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},')
package main
import (
"context"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
func main() {
// Obtain access credentials from environment variables. Before you run this code example, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
// Use the default configuration of the SDK, and specify the region, CloudBox ID, and data domain.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
// Specify the region in which the CloudBox is located, such as cn-hangzhou.
WithRegion("cn-hangzhou").
// Specify the data domain in the CloudboxId.Region.oss-cloudbox.aliyuncs.com format.
WithEndpoint("cb-f8z7yvzgwfkl9q0h****.cn-hangzhou.oss-cloudbox.aliyuncs.com").
// Specify the CloudBox ID. After you set the CloudBox ID, the SDK automatically switches the signing product to oss-cloudbox.
WithCloudBoxId("cb-f8z7yvzgwfkl9q0h****")
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a symbolic link. Key specifies the name of the symbolic link, and Target specifies the destination object to which the symbolic link points.
request := &oss.PutSymlinkRequest{
Bucket: oss.Ptr("examplebucket"),
Key: oss.Ptr("yourSymLink"),
Target: oss.Ptr("yourDestinationObjectName"),
}
result, err := client.PutSymlink(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put symlink %v", err)
}
log.Printf("put symlink result, status code:%d, request id:%s\n",
result.StatusCode, result.RequestID)
}
Use ossutil
To create a symbolic link by using ossutil, see put-symlink.
Use a REST API
If your program requires a high level of customization, you can send REST API requests directly. To do so, you must write code to calculate the signature. For more information, see PutSymlink.