Cette rubrique explique comment répertorier tous les objets, un nombre défini d'objets ou les objets portant un préfixe spécifique dans un compartiment (bucket) lorsque le versioning est activé.
Répertorier les informations sur tous les objets d'un compartiment
Le code suivant permet de répertorier les informations de version de tous les objets, y compris les marqueurs de suppression, dans un compartiment donné :
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 running this sample code, make sure 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'
});
// List the version information of all objects, including delete markers.
async function getObjectVersions() {
let nextKeyMarker = null;
let nextVersionMarker = null;
let versionListing = null;
do {
versionListing = await client.getBucketVersions({
keyMarker: nextKeyMarker,
versionIdMarker: nextVersionMarker,
});
versionListing.objects.forEach((o) => {
console.log(`${o.name}, ${o.versionId}`);
});
versionListing.deleteMarker.forEach((o) => {
console.log(`${o.name}, ${o.versionId}`);
});
nextKeyMarker = versionListing.NextKeyMarker;
nextVersionMarker = versionListing.NextVersionIdMarker;
} while (versionListing.isTruncated);
}
getObjectVersions();
Répertorier les informations de version des objets avec un préfixe spécifié
Le code suivant permet de répertorier les informations de version des objets portant un préfixe spécifique :
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 running this sample code, make sure 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'
});
// List the version information of objects that have the "test-" prefix.
async function getObjectVersionsByPrefix() {
let nextKeyMarker = null;
let nextVersionMarker = null;
let versionListing = null;
const prefix = 'test-'
do {
versionListing = await client.getBucketVersions({
keyMarker: nextKeyMarker,
versionIdMarker: nextVersionMarker,
prefix
})
versionListing.objects.forEach(o => {
console.log(`${o.name}, ${o.versionId}`)
})
versionListing.deleteMarker.forEach(o => {
console.log(`${o.name}, ${o.versionId}`)
})
nextKeyMarker = versionListing.NextKeyMarker;
nextVersionMarker = versionListing.NextVersionIdMarker;
} while (versionListing.isTruncated);
}
getObjectVersionsByPrefix();
Répertorier les informations de version d'un nombre défini d'objets
Le code suivant permet de répertorier les informations de version d'un nombre défini d'objets :
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 running this sample code, make sure 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 getObjectVersionByNumber() {
// List the version information of a maximum of 100 objects.
const versionListing = await client.getBucketVersions({
"max-keys": 100,
});
// Obtain the version information of the objects. If versioning is disabled, the VersionId is "none".
versionListing.objects.forEach((o) => {
console.log(`${o.name}, ${o.versionId}`);
});
versionListing.deleteMarker.forEach((o) => {
console.log(`${o.name}, ${o.versionId}`);
});
}
getObjectVersionByNumber();
Fonctionnalité de dossier
OSS ne propose pas de concept natif de dossier. Tous les éléments sont stockés sous forme d'objets. Vous pouvez simuler un dossier en créant un objet de 0 Ko dont le nom se termine par une barre oblique (/). Cet objet peut être chargé et téléchargé. La console OSS affiche comme des dossiers les objets dont le nom se termine par une barre oblique (/).
Utilisez les paramètres delimiter et prefix pour simuler la fonctionnalité de dossier :
Si vous définissez le paramètre
prefixsur un nom de dossier, OSS répertorie tous les objets dont le nom commence par le préfixe spécifié. Cette opération renvoie tous les objets du dossier et de ses sous-répertoires.Si vous définissez le paramètre
prefixet le paramètredelimitersur une barre oblique (/), OSS répertorie uniquement les objets et les sous-répertoires au premier niveau de ce dossier. Les sous-répertoires sont renvoyés dans l'élémentCommonPrefixeset les objets contenus dans ces sous-répertoires ne sont pas répertoriés.
Supposons qu'un compartiment contienne quatre objets : oss.jpg, fun/test.jpg, fun/movie/001.avi et fun/movie/007.avi4. La barre oblique (/) sert de séparateur de dossier. Les exemples suivants montrent comment répertorier les objets en simulant des dossiers.
Répertorier les informations de version des objets dans le répertoire racine
Le code suivant permet de répertorier les informations de version des objets dans le répertoire racine :
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 running this sample code, make sure 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'
});
// Set the delimiter parameter to a forward slash (/) to list the version information of objects and the names of folders in the root directory.
async function getRootObjectVersions() {
let nextKeyMarker = null;
let nextVersionMarker = null;
let versionListing = null;
do {
versionListing = await client.getBucketVersions({
keyMarker: nextKeyMarker,
versionIdMarker: nextVersionMarker,
delimiter: "/",
});
nextKeyMarker = versionListing.NextKeyMarker;
nextVersionMarker = versionListing.NextVersionIdMarker;
console.log(versionListing);
} while (versionListing.isTruncated);
}
getRootObjectVersions();
Répertorier les fichiers et les sous-répertoires dans un répertoire
Le code suivant permet de répertorier les objets et les sous-répertoires dans un répertoire spécifié :
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 running this sample code, make sure 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'
});
// Set the prefix parameter to get all files and folders in the foo/ directory. Also, set the delimiter parameter to a forward slash (/) to act as the folder separator.
async function getObjectVersionsByPrefixAndDirectory() {
let nextKeyMarker = null;
let nextVersionMarker = null;
let versionListing = null;
let prefix = "foo/";
do {
versionListing = await client.getBucketVersions({
keyMarker: nextKeyMarker,
versionIdMarker: nextVersionMarker,
prefix,
delimiter: "/",
});
nextKeyMarker = versionListing.NextKeyMarker;
nextVersionMarker = versionListing.NextVersionIdMarker;
console.log(versionListing);
} while (versionListing.isTruncated);
}
getObjectVersionsByPrefixAndDirectory();
Références
Pour plus d'informations sur l'opération API permettant de répertorier les objets, consultez ListObjectVersions (GetBucketVersions).