Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).
Prerequisites
Before you begin, make sure that you have:
An OSS bucket
Environment variables
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETset with valid access credentialsRead permissions on the symbolic link to query it
Create a symbolic link
The following example creates a symbolic link named symlinkobject.txt that points to exampleobject.txt.
const OSS = require('ali-oss');
const client = new OSS({
// Bucket region. For example, oss-cn-hangzhou for China (Hangzhou).
region: 'yourRegion',
// Access credentials loaded from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'examplebucket',
});
// Optional headers. Uncomment the headers object and pass it as the third
// argument to putSymlink to apply storage class, ACL, or overwrite settings.
//
// const headers = {
// 'x-oss-storage-class': 'Standard', // Storage class of the symbolic link object.
// 'x-oss-object-acl': 'private', // Access control list (ACL) of the object.
// 'x-oss-forbid-overwrite': 'true', // Set to 'true' to prevent overwriting an existing object with the same name.
// };
async function createSymlink() {
try {
const result = await client.putSymlink(
'symlinkobject.txt', // Name of the symbolic link to create.
'exampleobject.txt' // Name of the target object the symbolic link points to.
// , { headers } // Uncomment to apply optional headers defined above.
);
console.log(result);
} catch (err) {
console.error('Failed to create symbolic link:', err.name, err.message);
}
}
createSymlink();Query the object a symbolic link points to
Read permissions on the symbolic link are required to query it. The following example retrieves the target object name for symlinkobject.txt.
const OSS = require('ali-oss');
const client = new OSS({
// Bucket region. For example, oss-cn-hangzhou for China (Hangzhou).
region: 'yourRegion',
// Access credentials loaded from environment variables.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
bucket: 'examplebucket',
});
// Query the target object name that the symbolic link points to.
client.getSymlink('symlinkobject.txt').then(r => console.log(r));References
PutSymlink — API operation for creating a symbolic link
GetSymlink — API operation for querying a symbolic link