Manage symbolic links (OSS SDK for C# 1.0)
You can use symbolic links to quickly access frequently used files in an Object Storage Service (OSS) bucket. A symbolic link is similar to a Windows shortcut. After you create a symbolic link, you can use it to quickly open the source file. This topic describes how to manage symbolic links in a versioning-enabled bucket.
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization (C# SDK V1).
-
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 Grant a custom policy.
Create a symbolic link
A symbolic link can have multiple versions, and each version can point to a different target object. OSS automatically generates a version ID and returns it in the `x-oss-version-id` response header. You can create a symbolic link that points to the current version of the target object.
In a versioning-enabled bucket, you cannot create a symbolic link for a delete marker.
The following code shows how to create a symbolic link:
using Aliyun.OSS;
using System.Text;
using Aliyun.OSS.Common;
// 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.";
// Specify the region in which the bucket is located. For example, if the bucket is located 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 based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
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
The GetSymlink operation retrieves the current version of a symbolic link by default. You can specify a version ID to retrieve a specific version. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found error. The response header includes `x-oss-delete-marker = true` and the version ID `x-oss-version-id`.
To retrieve a symbolic link, you must have read permissions for it.
The following code shows how to retrieve a symbolic link:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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";
// Specify the region in which the bucket is located. For example, if the bucket is located 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 based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
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.