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:

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:

PlaceholderDescription
yourAccessKeyIdThe AccessKey ID of your STS temporary access credential
yourAccessKeySecretThe AccessKey secret of your STS temporary access credential
yourSecurityTokenThe security token of your STS temporary access credential
yourBucketNameThe name of the bucket to query

References