A symbolic link provides quick access to an object in a bucket and facilitates access to a frequently used object in Object Storage Service (OSS). A symbolic link works similarly to a shortcut in Windows. This topic describes how to manage symbolic links in a versioning-enabled bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To create a symbolic link, you must have the
oss:PutObjectpermission. To query a symbolic link, you must have theoss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Create a symbolic link
A symbolic link can have multiple versions. When you create a symbolic link in a versioning-enabled bucket, the version ID of the symbolic link is automatically generated by OSS and is returned as the x-oss-version-id header in the response. You can create a symbolic link to which the current version of an object points.
In a versioning-enabled bucket, a symbolic link cannot be created for a delete marker.
The following code provides an example on how to create a symbolic link:
using Aliyun.OSS;
using System.Text;
using Aliyun.OSS.Common;
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name.
var targetObjectName = "yourTargetObjectName";
// Specify the name of the symbolic link.
var symlinkObjectName = "yourSymlinkObjectName";
var objectContent = "More than just cloud.";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Upload the target object.
byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
MemoryStream requestContent = new MemoryStream(binaryData);
client.PutObject(bucketName, targetObjectName, requestContent);
// Create the symbolic link.
client.CreateSymlink(bucketName, symlinkObjectName, targetObjectName);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}Get a symbolic link
By default, the GetSymlink operation obtains the current version of a symbolic link. You can specify the version ID in the request to query the specified version of a symbolic link. If the current version of the symbolic link is a delete marker, OSS returns 404 Not Found and includes x-oss-delete-marker = true and x-oss-version-id in the response.
To query a symbolic link, you must have read permissions on the symbolic link.
The following code provides an example on how to query a symbolic link:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the symbolic link. The full path cannot contain the bucket name.
var symlinkObjectName = "yourSymlinkObjectName";
// Specify the version ID of the symbolic link.
var versionid = "yourArchiveObjectVersionid";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Get the name of the object file that the symbolic link points to.
var request = new GetSymlinkRequest(bucketName, symlinkObjectName)
{
// Get the content of a specific version of the symbolic link.
VersionId = versionid
};
var ossSymlink = client.GetSymlink(request);
Console.WriteLine("Target object is {0}", ossSymlink.Target);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}References
For more information about the API operation to create a symbolic link, see PutSymlink.
For more information about the API operation to retrieve a symbolic link, see GetSymlink.