Um bucket é um contêiner usado para armazenar objetos no Object Storage Service (OSS). Este tópico descreve como consultar informações sobre um bucket.
Código de exemplo
O código a seguir recupera informações de um bucket, como região e data de criação.
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 that 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,
// Set bucket to the name of your bucket.
bucket: 'yourBucketName',
});
async function getBucketInfo() {
// Specify the bucket name. For example, examplebucket.
const bucket = 'examplebucket'
const result = await client.getBucketInfo(bucket)
// Get the region of the bucket.
console.log(result.bucket.Location)
// Get the name of the bucket.
console.log(result.bucket.Name)
// Get the ID of the bucket owner.
console.log(result.bucket.Owner.ID)
// Get the name of the bucket owner. The name is the same as the owner ID.
console.log(result.bucket.Owner.DisplayName)
// Get the creation time of the bucket.
console.log(result.bucket.CreationDate)
// Get the storage class of the bucket.
console.log(result.bucket.StorageClass)
// Get the versioning state of the bucket.
console.log(result.bucket.Versioning)
}
getBucketInfo()
Referências
Para obter o código de exemplo completo sobre como recuperar informações do bucket, consulte Exemplo no GitHub.
Para mais detalhes sobre a operação de API usada para recuperar informações do bucket, consulte GetBucketInfo.