Un bucket est un conteneur utilisé pour stocker des objets dans Object Storage Service (OSS). Cette rubrique explique comment interroger les informations relatives à un bucket.
Exemple de code
Le code suivant récupère des informations sur un bucket, telles que sa région et sa date de création.
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()
Références
Pour l'exemple de code complet de récupération des informations d'un bucket, consultez l'exemple GitHub.
Pour plus d'informations sur l'opération API de récupération des informations d'un bucket, consultez GetBucketInfo.