Use the OSS Node.js SDK to get the region where a bucket is located.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with valid AccessKey credentials
Get the bucket region
Call getBucketInfo() and read result.bucket.Location from the response. The returned value is the region ID of the bucket, for example, oss-cn-hangzhou.
const OSS = require('ali-oss')
const client = new OSS({
accessKeyId: process.env.OSS_ACCESS_KEY_ID, // AccessKey ID
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, // AccessKey secret
region: 'oss-cn-hangzhou', // Region where the bucket is located
authorizationV4: true, // Use the V4 signature algorithm
bucket: 'yourBucketName', // Name of the bucket
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com', // Public endpoint of the region
});
async function getLocation() {
try {
const result = await client.getBucketInfo();
console.log(result.bucket.Location); // Example output: oss-cn-hangzhou
} catch (e) {
console.log(e);
}
}
getLocation();Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
yourBucketName | Name of the bucket | my-bucket |
oss-cn-hangzhou | Region ID where the bucket is located | oss-cn-hangzhou |
https://oss-cn-hangzhou.aliyuncs.com | Public endpoint of the region | https://oss-cn-hangzhou.aliyuncs.com |
What's next
For the complete sample code, see the GitHub example.
For the underlying API, see GetBucketLocation.