Les liens symboliques fonctionnent comme des raccourcis de fichiers sous Windows et permettent d'accéder rapidement aux objets associés dans Object Storage Service (OSS).
Créer un lien symbolique
L'exemple de code suivant montre comment créer un lien symbolique :
const OSS = require('ali-oss')
const client = new OSS({
// 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 oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the bucket.
bucket: 'examplebucket',
});
const headers = {
// Specify the storage class of the object.
'x-oss-storage-class':'Standard',
// Specify the access control list (ACL) of the object.
'x-oss-object-acl':'private',
// Specify whether to overwrite the object that has the same name as the symbolic link. In this example, this parameter is set to true, which indicates that the object with the same name cannot be overwritten.
'x-oss-forbid-overwrite': 'true '
};
async function put () {
try {
// Specify symlinkobject.txt as the name of the symbolic link and exampleobject.txt as the name of the object to which you want the symbolic link to point.
const result = await client.putSymlink('symlinkobject.txt', 'exampleobject.txt'
// ,{ headers }
);
console.log(result);
} catch (e) {
console.log(e);
}
}
put();
Récupérer le nom de l'objet ciblé par le lien symbolique
Pour récupérer un lien symbolique, vous devez disposer des autorisations de lecture sur ce lien. Le code suivant montre comment obtenir le nom de l'objet ciblé par le lien symbolique :
const OSS = require('ali-oss')
const client = new OSS({
// 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 oss-cn-hangzhou.
region: 'yourRegion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the bucket.
bucket: 'examplebucket',
});
// Specify the name of the symbolic link and query the name of the object to which the symbolic link points.
client.getSymlink('symlinkobject.txt').then(r=>console.log(r))
Références
Pour en savoir plus sur l'opération API permettant de créer un lien symbolique, consultez PutSymlink.
Pour en savoir plus sur l'opération API permettant de récupérer un lien symbolique, consultez GetSymlink.