Um bucket é um contêiner usado para armazenar objetos no Object Storage Service (OSS). Este tópico descreve como consultar informações de um bucket.
Observações
Neste tópico, usamos o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, obtemos as credenciais de acesso a partir de variáveis de ambiente. Para obter mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Initialization.
Para consultar informações de um bucket, você deve ter a permissão
oss:GetBucketInfo. Para obter mais informações, consulte Grant a custom policy.
Exemplos
O código a seguir mostra como consultar informações de um bucket, incluindo a região e a data de criação do bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Query information about the bucket.
bucket_info = bucket.get_bucket_info()
# Query the name of the bucket.
print("name: " + bucket_info.name)
# Query the storage class of the bucket.
print("storage class: " + bucket_info.storage_class)
# Query the time when the bucket was created.
print("creation date: " + bucket_info.creation_date)
# Query the internal endpoint of the bucket.
print("intranet_endpoint: " + bucket_info.intranet_endpoint)
# Query the public endpoint of the bucket.
print("extranet_endpoint " + bucket_info.extranet_endpoint)
# Query the owner of the bucket.
print("owner: " + bucket_info.owner.id)
# Query the access control list (ACL) of the bucket.
print("grant: " + bucket_info.acl.grant)
# Query the redundancy type of the bucket.
print("data_redundancy_type:" + bucket_info.data_redundancy_type)
# Query the access tracking status of the bucket. Only OSS SDK for Python 2.16.1 and later allow you to query the access tracking status.
print("access_monitor:" + bucket_info.access_monitor)
Referências
Para acessar o código de exemplo completo usado para consultar informações de um bucket, visite o GitHub.
Para obter mais informações sobre a operação da API usada para consultar informações de um bucket, consulte GetBucketInfo.