Um bucket é um contêiner para objetos armazenados no Object Storage Service (OSS). Todos os objetos no OSS residem em buckets, listados em ordem alfabética. Você pode listar os buckets da conta atual da Alibaba Cloud em todas as regiões que atendam a condições específicas.
Observações de uso
Antes de executar o código de exemplo deste tópico, crie uma instância OSSClient usando métodos como nome de domínio personalizado ou Security Token Service (STS). Para mais informações, consulte Inicialização (Android SDK).
Listar todos os buckets de uma conta Alibaba Cloud
O código de exemplo a seguir mostra como listar buckets em todas as regiões da conta atual da Alibaba Cloud:
// List all buckets in all regions of your account.
ListBucketsRequest request = new ListBucketsRequest();
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
@Override
public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
List<OSSBucketSummary> buckets = result.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Log.i("info", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Listar buckets cujos nomes contenham um prefixo especificado
O exemplo a seguir lista buckets com o prefixo example em todas as regiões da conta atual da Alibaba Cloud:
O prefixo permite correspondência parcial. A consulta retorna todos os buckets cujos nomes começam com o prefixo definido. Por exemplo, se você definir o prefixo como a, buckets como abucket e abcbucket serão retornados.
// Specify the URL of the STS application server. For example, http://example.com.
String stsServer = "http://example.com";
// Use OSSAuthCredentialsProvider to automatically update the token when it expires.
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);
ListBucketsRequest request = new ListBucketsRequest();
// List buckets with the prefix 'example' in all regions of your account.
request.setPrefix("example");
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
@Override
public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
List<OSSBucketSummary> buckets = result.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Log.i("i", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Listar buckets posteriores ao bucket indicado pelo marker em ordem alfabética
O código abaixo lista buckets cujos nomes aparecem, em ordem alfabética, após o bucket chamado examplebucket em todas as regiões da conta atual da Alibaba Cloud:
Consultas baseadas em prefixo retornam todos os buckets cujos nomes começam com o valor especificado. Por exemplo, uma consulta com o prefixo a inclui buckets como abucket e abcbucket.
O resultado contém o primeiro bucket retornado pela correspondência parcial e todos os subsequentes.
// Specify the URL of the STS application server. For example, http://example.com.
String stsServer = "http://example.com";
// Use OSSAuthCredentialsProvider to automatically update the token when it expires.
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);
ListBucketsRequest request = new ListBucketsRequest();
// List buckets that come after 'examplebucket' in alphabetical order in all regions of your account.
request.setMarker("examplebucket");
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
@Override
public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
List<OSSBucketSummary> buckets = result.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Log.i("i", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Listar uma quantidade específica de buckets
Para limitar a listagem de buckets em todas as regiões da conta atual da Alibaba Cloud a um máximo de 500 itens, use o seguinte exemplo:
// Specify the URL of the STS application server. For example, http://example.com.
String stsServer = "http://example.com";
// Use OSSAuthCredentialsProvider to automatically update the token when it expires.
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);
ClientConfiguration config = new ClientConfiguration();
OSSClient ossClient = new OSSClient(getApplicationContext(), credentialProvider, config);
ListBucketsRequest request = new ListBucketsRequest();
// List buckets in all regions of your account and limit the number of buckets returned to 500. The default value is 100. The maximum value is 1000.
request.setMaxKeys(500);
ossClient.asyncListBuckets(request, new OSSCompletedCallback<ListBucketsRequest, ListBucketsResult>() {
@Override
public void onSuccess(ListBucketsRequest request, ListBucketsResult result) {
List<OSSBucketSummary> buckets = result.getBuckets();
for (int i = 0; i < buckets.size(); i++) {
Log.i("i", "name: " + buckets.get(i).name + " "
+ "location: " + buckets.get(i).location);
}
}
@Override
public void onFailure(ListBucketsRequest request, ClientException clientException, ServiceException serviceException) {
// The request failed.
if (clientException != null) {
// A client exception occurred, such as a network exception.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server exception occurred.
Log.e("ErrorCode", serviceException.getErrorCode());
Log.e("RequestId", serviceException.getRequestId());
Log.e("HostId", serviceException.getHostId());
Log.e("RawMessage", serviceException.getRawMessage());
}
}
});
Referências
Para obter o código de exemplo completo, consulte o exemplo no GitHub.
Para consultar a referência da API desta operação, veja ListBuckets (GetService).