A bucket is a container used to store objects in Object Storage Service (OSS). This topic describes how to query information about a bucket.
Sample code
The following code retrieves information about a bucket, such as its region and creation date.
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()References
For the complete sample code for retrieving bucket information, see GitHub example.
For more information about the API operation to retrieve bucket information, see GetBucketInfo.