Um bucket é um contêiner para objetos armazenados no Object Storage Service (OSS). Todos os objetos no OSS estão contidos em buckets. Os buckets são listados em ordem alfabética. É possível listar buckets pertencentes à conta Alibaba Cloud atual em todas as regiões e que atendam a condições específicas.
Notas de uso
Antes de executar o código de exemplo deste tópico, crie uma instância OSSClient usando métodos como domínio personalizado ou Security Token Service (STS). Para mais informações, consulte Initialization (Android SDK).
Listar todos os buckets de uma conta Alibaba Cloud
O exemplo de código a seguir demonstra como listar buckets em todas as regiões da conta Alibaba Cloud atual:
// 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 contêm um prefixo específico
O exemplo de código a seguir demonstra como listar buckets cujos nomes contêm o prefixo example em todas as regiões da conta Alibaba Cloud atual:
O prefixo é usado para correspondência aproximada. A consulta retorna todos os buckets cujos nomes começam com o prefixo especificado. Por exemplo, ao definir o prefixo como a, buckets como abucket e abcbucket sã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 cujos nomes são alfabeticamente posteriores ao bucket especificado pelo marker
O exemplo de código a seguir demonstra como listar buckets cujos nomes são alfabeticamente posteriores ao bucket examplebucket em todas as regiões da conta Alibaba Cloud atual:
Consultas baseadas em prefixo retornam todos os buckets cujos nomes começam com o prefixo especificado. Por exemplo, uma consulta com o prefixo a retorna buckets como abucket e abcbucket.
O resultado da consulta inclui o primeiro bucket retornado pela correspondência aproximada e todos os buckets 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
O exemplo de código a seguir demonstra como listar buckets em todas as regiões da conta Alibaba Cloud atual e definir o número máximo de buckets retornados como 500:
// 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 o código de exemplo completo, consulte o exemplo no GitHub.
Para a referência de API desta operação, consulte ListBuckets (GetService).