Um bucket é um contêiner que armazena objetos no Object Storage Service (OSS). Todo objeto reside em um bucket. Este tópico descreve como consultar a região de um bucket.
Pré-requisitos
Instale o SDK
ali-oss.Configure as variáveis de ambiente
OSS_ACCESS_KEY_IDeOSS_ACCESS_KEY_SECRET. O código de exemplo obtém as credenciais de acesso dessas variáveis de ambiente.
Código de exemplo
O código de exemplo a seguir mostra como consultar a região ou localização de um bucket:
const OSS = require('ali-oss');
const client = new OSS({
// Obtain the access credentials from environment variables. Before you run this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the region of the bucket. For example, if the bucket is in the China (Hangzhou) region, specify oss-cn-hangzhou. Replace the example value with the actual region of your bucket.
region: 'oss-cn-hangzhou',
// Use the V4 signature algorithm.
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
// Specify the public endpoint of the region where the bucket resides. For example, if the bucket is in the China (Hangzhou) region, specify https://oss-cn-hangzhou.aliyuncs.com. Replace the example value with the actual endpoint of your bucket.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
});
async function getLocation() {
try {
const result = await client.getBucketInfo();
console.log(result.bucket.Location);
} catch (e) {
console.log(e);
}
}
getLocation();
O exemplo exibe o valor do campo Location, que indica a região do bucket. Por exemplo, se o bucket estiver na região China (Hangzhou), a saída será oss-cn-hangzhou.
Referências
Para obter o código de exemplo completo, consulte o exemplo no GitHub.
Para obter mais informações sobre a operação de API usada neste exemplo, consulte GetBucketLocation.