Use a operação GetBucketLocation para obter a região de um bucket com o OSS SDK for C# V2.
Observações
O código de exemplo deste tópico usa o ID da região China (Hangzhou)
cn-hangzhoue o endpoint público. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use o endpoint interno. Para mais informações sobre o mapeamento entre regiões e endpoints, consulte Regiões e endpoints.O código de exemplo lê as credenciais de acesso das variáveis de ambiente. Para saber como configurar as credenciais de acesso, consulte [Configurar credenciais de acesso para .NET SDK 2.0].
Para obter a região de um bucket, você precisa da permissão
oss:GetBucketLocation. Para mais detalhes, consulte Conceder permissões personalizadas a um usuário RAM.
Código de exemplo
O código a seguir obtém a região de um bucket.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Set the region in which the bucket is located. This topic uses China (Hangzhou) as an example. Set Region to cn-hangzhou.
var bucket = "your bucket name"; // Required. Set the name of the destination bucket.
var endpoint = null as string; // Optional. Specify the domain name used to access OSS. This topic uses China (Hangzhou) as an example. Set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of the OSS SDK. These configurations automatically read credential information, such as an AccessKey, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly specify that environment variables are used to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);
// Call the GetBucketLocationAsync method to obtain the region information of the specified bucket.
var result = await client.GetBucketLocationAsync(new OSS.Models.GetBucketLocationRequest()
{
Bucket = bucket
});
// Print the result.
Console.WriteLine("GetBucketLocation done"); // Indicate that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used by Alibaba Cloud to troubleshoot issues.
Console.WriteLine("Response Headers:"); // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
Console.WriteLine($"Location: {result.LocationConstraint}"); // Print the region to which the bucket belongs.
Referências
Para ver o código de exemplo completo, consulte GetBucketLocation.cs.