Get bucket information (Harmony SDK)
Updated at:
Use the OSS Harmony SDK to retrieve metadata about a bucket, including its region and creation date.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket. For supported regions and their endpoints, see Regions and endpoints.
The
oss:GetBucketInfopermission granted to your Resource Access Management (RAM) user. To grant it to a RAM user, see Attach a custom policy to a RAM user.
Get bucket information
The following example calls getBucketInfo and logs key properties from the response, including the bucket's region and creation date.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client using STS temporary access credentials.
const client = new Client({
// Replace with your STS temporary AccessKey ID.
accessKeyId: 'yourAccessKeyId',
// Replace with your STS temporary AccessKey secret.
accessKeySecret: 'yourAccessKeySecret',
// Replace with your STS security token.
securityToken: 'yourSecurityToken',
// Replace with the region where your bucket is located, for example, oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
const getBucketInfo = async () => {
try {
const res = await client.getBucketInfo({
bucket: 'yourBucketName',
});
// Print the result.
console.log(JSON.stringify(res));
} catch (err) {
if (err instanceof RequestError) {
console.log('Error code:', err.code);
console.log('Error message:', err.message);
console.log('Request ID:', err.requestId);
console.log('HTTP status code:', err.status);
console.log('Error category:', err.ec);
} else {
console.log('Unknown error:', err);
}
}
};
getBucketInfo();Replace the following placeholders before running the code:
| Placeholder | Description |
|---|---|
yourAccessKeyId | The AccessKey ID of your STS temporary access credential |
yourAccessKeySecret | The AccessKey secret of your STS temporary access credential |
yourSecurityToken | The security token of your STS temporary access credential |
yourBucketName | The name of the bucket to query |
References
GetBucketInfo — API operation reference
Is this page helpful?