Consulte a região em que um bucket está localizado.
Observações de uso
O código de exemplo usa o ID da região
cn-hangzhou, referente à região China (Hangzhou). Por padrão, o acesso aos recursos do bucket ocorre pelo endpoint público. Para acessar esses recursos por meio de outros serviços da Alibaba Cloud na mesma região do bucket, use o endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.O código de exemplo obtém as credenciais de acesso das variáveis de ambiente. Para obter mais informações, consulte Configurar credenciais de acesso.
Para consultar a região de um bucket, você precisa da permissão
oss:GetBucketLocation. Para obter mais informações, consulte Conceder uma política personalizada.
Método
func (c *Client) GetBucketLocation(ctx context.Context, request *GetBucketLocationRequest, optFns ...func(*Options)) (*GetBucketLocationResult, error)
Parâmetros da solicitação
|
Parâmetro |
Tipo |
Descrição |
|
ctx |
context.Context |
Contexto da solicitação. Use este parâmetro para especificar a duração total da operação. |
|
request |
*GetBucketLocationRequest |
Parâmetros da solicitação, como o nome do bucket. Para obter mais informações, consulte GetBucketLocationRequest. |
|
optFns |
...func(*Options) |
Opcional. Parâmetro no nível da operação. Para obter mais informações, consulte Options. |
Parâmetros de resposta
|
Parâmetro de resposta |
Tipo |
Descrição |
|
result |
*GetBucketLocationResult |
Resposta da operação. Válido quando err é nil. Para obter mais informações, consulte GetBucketLocationResult. |
|
err |
error |
Mensagem de erro. Um valor diferente de nil indica falha na solicitação. |
Exemplos
O código de exemplo a seguir consulta a região de um bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Specify the global variables.
var (
region string // The region.
bucketName string // The name of the bucket.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The `name` of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to query the region of the bucket.
request := &oss.GetBucketLocationRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
}
// Execute the request to query the region of the bucket and process the result.
result, err := client.GetBucketLocation(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get bucket location %v", err)
}
// Display the region of the bucket.
log.Printf("get bucket location:%#v\n", *result.LocationConstraint)
}
Referências
Para obter o código de exemplo completo, acesse o GitHub.
Para obter a referência da API, consulte GetBucketLocation.