Todos os produtos
Search
Central de documentação

Object Storage Service:Listar objetos (Android SDK)

Última atualização: Jul 03, 2026

Este tópico descreve como listar objetos em um bucket, incluindo todos os objetos do bucket, uma quantidade específica de objetos e objetos cujos nomes contêm um prefixo específico.

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 uma quantidade específica de objetos

O código de exemplo a seguir mostra como listar 20 objetos em um bucket chamado examplebucket:

// Specify the bucket name, for example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the maximum number of objects to return. If you do not set this parameter, the default value is 100. The value of maxkeys cannot be greater than 1,000.
request.setMaxKeys(20);

oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
    @Override
    public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
        for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
            Log.i("ListObjects", objectSummary.getKey());
        }
    }

    @Override
    public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network error.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Listar objetos cujos nomes contêm um prefixo específico

O código de exemplo a seguir mostra como listar objetos com o prefixo file no nome em um bucket chamado examplebucket:

// Specify the bucket name, for example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the prefix.
// The prefix is used for fuzzy matching. The query returns objects whose names start with the specified prefix. For example, if you set the prefix to "a", all objects whose names start with "a", such as abc.txt and abcd.jpg, are returned.
request.setPrefix("file");

oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
    @Override
    public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
        for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
            Log.i("ListObjects", objectSummary.getKey());
        }
    }

    @Override
    public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network error.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Listar objetos cujos nomes são alfabeticamente posteriores ao valor de marker

O código de exemplo a seguir mostra como listar objetos cujos nomes vêm depois, em ordem alfabética, do objeto chamado exampleobject.txt em um bucket chamado examplebucket:

// Specify the bucket name, for example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the marker. The list operation starts from the first object that is alphabetically after the marker.
// If the specified marker does not exist in the bucket, the list operation starts from the object that is alphabetically after the marker value.
request.setMarker("exampleobject.txt");

oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
    @Override
    public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
        for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
            Log.i("ListObjects", objectSummary.getKey());
        }
    }

    @Override
    public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network error.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Listar todos os objetos de um bucket por página

O código de exemplo a seguir mostra como listar todos os objetos em um bucket chamado examplebucket por página. Cada página exibe até 20 objetos.

private String marker = null;
private boolean isCompleted = false;

// List all objects by page.
public void getAllObject() {
    do {
        OSSAsyncTask task = getObjectList();
        // Block the thread and wait for the request to complete to get NextMarker. To request the next page, set the marker to the NextMarker value from the previous response. You do not need to set a marker for the first page.
        // In this example, a loop is used to list data by page. You must block the thread and wait for the request to complete to get NextMarker before you can request the next page. Determine whether to block the thread based on your scenario.
        task.waitUntilFinished();
    } while (!isCompleted);
}

// List one page of objects.
public OSSAsyncTask getObjectList() {
    // Specify the bucket name.
    ListObjectsRequest request = new ListObjectsRequest("examplebucket");
    // Specify the maximum number of objects to return per page. If you do not set this parameter, the default value is 100. The value of maxkeys cannot be greater than 1,000.
    request.setMaxKeys(20);
    request.setMarker(marker);

    OSSAsyncTask task = oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
        @Override
        public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
            for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
                Log.i("ListObjects", objectSummary.getKey());
            }
            // This is the last page.
            if (!result.isTruncated()) {
                isCompleted = true;
                return;
            }
            // The marker for the next list operation.
            marker = result.getNextMarker();
        }

        @Override
        public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
            isCompleted = true;
            // Request exception.
            if (clientException != null) {
                // Client exception, such as a network error.
                clientException.printStackTrace();
            }
            if (serviceException != null) {
                // Server exception.
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    return task;
}

Listar objetos cujos nomes contêm um prefixo específico por página

O código de exemplo a seguir mostra como listar objetos com o prefixo file no nome em um bucket chamado examplebucket por página. Cada página exibe até 20 objetos.

private String marker = null;
private boolean isCompleted = false;

// List all objects by page.
public void getAllObject() {
    do {
        OSSAsyncTask task = getObjectList();
        // Block the thread and wait for the request to complete to get NextMarker. To request the next page, set the marker to the NextMarker value from the previous response. You do not need to set a marker for the first page.
        // In this example, a loop is used to list data by page. You must block the thread and wait for the request to complete to get NextMarker before you can request the next page. Determine whether to block the thread based on your scenario.
        task.waitUntilFinished();
    } while (!isCompleted);
}

// List one page of objects.
public OSSAsyncTask getObjectList() {
    // Specify the bucket name.
    ListObjectsRequest request = new ListObjectsRequest("examplebucket");
    // Specify the maximum number of objects to return per page. If you do not set this parameter, the default value is 100. The value of maxkeys cannot be greater than 1,000.
    request.setMaxKeys(20);
    // Specify the prefix.
    // The prefix is used for fuzzy matching. The query returns objects whose names start with the specified prefix. For example, if you set the prefix to "a", all objects whose names start with "a", such as abc.txt and abcd.jpg, are returned.
    request.setPrefix("file");
    request.setMarker(marker);

    OSSAsyncTask task = oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
        @Override
        public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
            for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
                Log.i("ListObjects", objectSummary.getKey());
            }
            // This is the last page.
            if (!result.isTruncated()) {
                isCompleted = true;
                return;
            }
            // The marker for the next list operation.
            marker = result.getNextMarker();
        }

        @Override
        public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
            isCompleted = true;
            // Request exception.
            if (clientException != null) {
                // Client exception, such as a network error.
                clientException.printStackTrace();
            }
            if (serviceException != null) {
                // Server exception.
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    return task;
}

Listar objetos cujos nomes contêm caracteres especiais

Se o nome de um objeto contiver algum dos caracteres especiais listados abaixo, codifique-o antes da transmissão. O OSS oferece suporte apenas à codificação URL.

  • Aspas simples (')

  • Aspas duplas (")

  • E comercial (&)

  • Sinais de maior e menor que (<>)

  • Caracteres chineses

O código de exemplo a seguir mostra como listar objetos com caracteres especiais no nome em um bucket chamado examplebucket:

// Specify the bucket name, for example, examplebucket.
ListObjectsRequest request = new ListObjectsRequest("examplebucket");
// Specify the encoding type for object names.
request.setEncodingType("url");

oss.asyncListObjects(request, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
    @Override
    public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
        for (OSSObjectSummary objectSummary : result.getObjectSummaries()) {
            Log.i("ListObjects", URLDecoder.decode(objectSummary.getKey(), "UTF-8"));
        }
    }

    @Override
    public void onFailure(ListObjectsRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network error.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Referências