Este tópico descreve como listar todos os objetos, um número especificado de objetos ou objetos com um prefixo específico em um bucket quando o versionamento está ativado.
Listar informações de todos os objetos em um bucket
O código a seguir mostra como listar as informações de versão de todos os objetos, incluindo marcadores de exclusão, em um bucket especificado:
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();
Listar informações de versão de objetos com um prefixo especificado
O código a seguir mostra como listar as informações de versão de objetos que possuem um prefixo especificado:
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();
Listar informações de versão de um número especificado de objetos
O código a seguir mostra como listar as informações de versão de um número especificado de objetos:
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();
Recurso de pastas
O OSS não possui um conceito nativo de pastas. Em vez disso, todos os elementos são armazenados como objetos. Você pode simular uma pasta criando um objeto de 0 KB cujo nome termina com uma barra (/). Esse objeto pode ser enviado e baixado. O console do OSS exibe objetos cujos nomes terminam com uma barra (/) como pastas.
Use os parâmetros `delimiter` e `prefix` para simular a funcionalidade de pastas:
-
Se você definir o parâmetro `prefix` como o nome de uma pasta, o OSS lista todos os objetos cujos nomes começam com o prefixo especificado. Isso retorna todos os objetos na pasta e em seus subdiretórios.
-
Se você definir o parâmetro `prefix` e definir o parâmetro `delimiter` como uma barra (/), o OSS lista apenas os objetos e subdiretórios no nível superior dessa pasta. Os subdiretórios são retornados no elemento `CommonPrefixes`, e os objetos dentro desses subdiretórios não são listados.
Suponha que um bucket contenha quatro objetos: oss.jpg, fun/test.jpg, fun/movie/001.avi e fun/movie/007.avi4. A barra (/) é usada como separador de pastas. Os exemplos a seguir mostram como listar objetos simulando pastas.
Listar informações de versão de objetos no diretório raiz
O código a seguir mostra como listar as informações de versão dos objetos no diretório raiz:
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();
Listar arquivos e subdiretórios em um diretório
O código a seguir mostra como listar os objetos e subdiretórios em um diretório especificado:
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();
Referências
Para mais informações sobre a operação da API para listar objetos, consulte ListObjectVersions (GetBucketVersions).