Manage symbolic links (Node.js SDK)
The symbolic link feature provides a convenient way to access frequently used objects in a bucket. You can use a symbolic link as a shortcut to access the target object.
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:
const OSS = require("ali-oss");
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
async function putSymlink() {
// Specify the name of the symbolic link to create.
const symlinkName = "symlinkName";
// Specify the target object for the symbolic link.
const targetName = "targetName";
// Specify the versionId of the target object.
const versionId = "versionId";
const result = await client.putSymlink(symlinkName, targetName, {
versionId,
});
console.log(result);
}
putSymlink();
Get 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:
const OSS = require("ali-oss");
const client = new OSS({
// Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
async function getSymlink() {
// Specify the name of the symbolic link to get.
const targetName = "targetName";
// Specify the versionId of the symbolic link to get.
const versionId = "versionId";
// Get the content of the symbolic link for the specified versionId.
const result = await client.getSymlink(targetName, {
versionId,
});
console.log(result);
}
getSymlink();
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.