Use getBucketInfo to retrieve metadata about an Object Storage Service (OSS) bucket, including its region, storage class, versioning state, creation date, and owner details.
Prerequisites
Before you begin, make sure you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid access key credentials
Get bucket information
The following example calls getBucketInfo and reads the returned fields from the response object.
const OSS = require('ali-oss');
// import OSS from 'ali-oss'; // ESM import
const client = new OSS({
region: '<your-region>', // required — for example, oss-cn-hangzhou
accessKeyId: process.env.OSS_ACCESS_KEY_ID, // required
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, // required
authorizationV4: true,
bucket: '<your-bucket-name>', // required
});
async function getBucketInfo() {
const result = await client.getBucketInfo('<your-bucket-name>');
// result.bucket = {
// Location: 'oss-cn-hangzhou', // region where the bucket is located
// Name: 'examplebucket', // bucket name
// Owner: {
// ID: '...', // owner account ID
// DisplayName: '...', // same as owner ID
// },
// CreationDate: '...', // bucket creation date
// StorageClass: 'Standard', // storage class
// Versioning: '', // versioning state
// }
console.log(result.bucket.Location);
console.log(result.bucket.Name);
console.log(result.bucket.Owner.ID);
console.log(result.bucket.Owner.DisplayName);
console.log(result.bucket.CreationDate);
console.log(result.bucket.StorageClass);
console.log(result.bucket.Versioning);
}
getBucketInfo();Replace the following placeholders with actual values:
| Placeholder | Description | Example |
|---|---|---|
<your-region> | Region where the bucket is located | oss-cn-hangzhou |
<your-bucket-name> | Name of your bucket | examplebucket |
References
For the complete sample code, see the GitHub example.
For the underlying API operation, see GetBucketInfo.