This topic describes how to manage symbolic links in a versioning-enabled bucket.

Create a symbolic link

You can create a symbol link that directs to the current version of the target object.
Note In a bucket with the versioning function enabled, you cannot create a symbol link for a delete marker.

A symbol link can has multiple versions that direct to different target objects. The version ID of the symbol link is automatically generated and is returned as the x-oss-version-id field in the response header.

The following code provides an example on how to create a symbolic link:

const OSS = require('ali-oss');

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function putSymlink() {
   // Create a symbolic link.
  const name = '<your objectName>'
  const targetName = '<your target objectName>'

  const result = await client.putSymlink(name, targetName);
  console.log(result.res.headers['x-oss-version-id']);
}

putSymlink();

For more information about creating a symbol link, see PutSymlink.

Obtain a symbolic link

GetSymlink obtains the current version of the target symbol link by default. You can specify the versionId in the request to obtain the specified version of a symbol link. If the current version of the target symbol link is a delete marker, OSS returns the 404 Not Found error and includes x-oss-delete-marker = true and x-oss-version-id in the response header.
Note You must have the read permission on a symbol link to obtain it.

The following code provides an example on how to obtain a symbolic link:

const OSS = require('ali-oss');

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function getSymlink() {
  const name = '<your objectName>'
  const versionId = '<your versionId>' 

  // Obtain the content of the symbolic link with the specified version ID.
  const result = await client.getSymlink(name, {
    versionId
  });
  console.log(result);
}

getSymlink();

For more information about obtaining a symbol link, see GetSymlink.