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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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:PutObject
permission. To query a symbolic link, you must have theoss:GetObject
permission. 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;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located 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 configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
var targetObjectName = "yourTargetObjectName";
// Specify the name of the symbolic link.
var symlinkObjectName = "yourSymlinkObjectName";
var objectContent = "More than just cloud.";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Upload the 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);
}
Query 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;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located 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 configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path.
var symlinkObjectName = "yourSymlinkObjectName";
// Specify the version ID of the symbolic link that you want to query.
var versionid = "yourArchiveObjectVersionid";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Query the name of the object to which the symbolic link points.
var request = new GetSymlinkRequest(bucketName, symlinkObjectName)
{
// Query the specified version of a 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 that you can call to create a symbolic link, see PutSymlink.
For more information about the API operation that you can call to query a symbolic link, see GetSymlink.