Get bucket information (Harmony SDK)
Updated at:
A bucket is a container used to store objects in Object Storage Service (OSS). This topic describes how to query information about a bucket.
Usage notes
-
For more information about the regions and endpoints that OSS supports, see Regions and endpoints.
-
If you use a Resource Access Management (RAM) user to obtain bucket information, grant the
oss:GetBucketInfopermission to the RAM user. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code shows how to obtain information about a bucket, such as its region and creation date.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client.
const client = new Client({
// Replace with the AccessKey ID of your STS temporary access credential.
accessKeyId: 'yourAccessKeyId',
// Replace with the AccessKey secret of your STS temporary access credential.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the security token of your STS temporary access credential.
securityToken: 'yourSecurityToken',
// Specify 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: 'oss-cn-hangzhou',
});
// Get detailed information about the bucket.
const getBucketInfo = async () => {
try {
// Call the getBucketInfo method to get detailed information about the specified bucket.
const res = await client.getBucketInfo({
bucket: 'yourBucketName' // Replace with your bucket name.
});
// Print the result.
console.log(JSON.stringify(res));
} catch (err) {
// Catch and handle request errors.
if (err instanceof RequestError) {
console.log('Error code: ', err.code); // The error code.
console.log('Error message: ', err.message); // The error description.
console.log('Request ID: ', err.requestId); // The unique ID of the request.
console.log('HTTP status code: ', err.status); // The HTTP response status code.
console.log('Error category: ', err.ec); // The error category.
} else {
console.log('Unknown error: ', err); // An error that is not of the RequestError type.
}
}
};
// Call the function to get detailed information about the bucket.
getBucketInfo();
References
-
For more information about the API operation to obtain bucket information, see GetBucketInfo.
Is this page helpful?