tencent cloud

Cloud Object Storage

Listing Objects

Download
Modo Foco
Tamanho da Fonte
Última atualização: 2026-05-15 15:04:05

Introduction

This document provides example code and a description for listing objects using the COS Android SDK.

Must-Knows

To use the list objects feature, you must first have read permission for the target bucket. When you configure the authorization policy, set the action to cos:GetBucket. For more information on authorization, see CAM-supported business APIs.
To use the list object versions feature, you must first have read permission for the target bucket. When you configure the authorization policy, set the action to cos:GetBucketObjectVersions. For more information on authorization, see CAM-supported business APIs.

Related Examples

Feature Name
Description
Example code
List objects.
Provides the capability to query some or all objects in a bucket.
List object versions.
Provides the capability to query some or all objects and their historical versions in a bucket.

Querying the Object List

Query some or all objects in a bucket.

Prerequisites: Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Creating CosXmlService.

Use Case: Obtaining the First Page of Data

String bucketName = "examplebucket-1250000000"; // Format: BucketName-APPID;
final GetBucketRequest getBucketRequest = new GetBucketRequest(bucketName);

// Prefix matching, used to specify the prefix address of the returned objects.
getBucketRequest.setPrefix("dir/");

// The maximum number of entries returned in a single response, with a default value of 1,000.
getBucketRequest.setMaxKeys(100);

cosXmlService.getBucketAsync(getBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketResult getBucketResult = (GetBucketResult) result;
if (getBucketResult.listBucket.isTruncated) {
// Indicates that data is truncated and the next page of data needs to be fetched.
prevPageResult = getBucketResult;
}
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Requesting the Next Page of Data

String bucketName = "examplebucket-1250000000"; // Format: BucketName-APPID;

GetBucketRequest getBucketRequest = new GetBucketRequest(bucketName);

// Prefix matching, used to specify the prefix address of the returned objects.
getBucketRequest.setPrefix("dir/");

// prevPageResult is the result returned from the previous page, and the nextMarker here indicates the starting position of the next page.
String nextMarker = prevPageResult.listBucket.nextMarker;
getBucketRequest.setMarker(nextMarker);

// The maximum number of entries returned in a single response, with a default value of 1,000.
getBucketRequest.setMaxKeys(100);

cosXmlService.getBucketAsync(getBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketResult getBucketResult = (GetBucketResult) result;
if (getBucketResult.listBucket.isTruncated) {
// Indicates that data is truncated and the next page of data needs to be fetched.
}
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Obtaining the Object List and Subdirectories

String bucketName = "examplebucket-1250000000"; // Format: BucketName-APPID;
GetBucketRequest getBucketRequest = new GetBucketRequest(bucketName);

// Prefix matching, used to specify the prefix address of the returned objects.
getBucketRequest.setPrefix("dir/");

// The maximum number of entries returned in a single response, with a default value of 1,000.
getBucketRequest.setMaxKeys(100);

// The delimiter is a single character. If a Prefix is present,
// Then, identical paths between the Prefix and the delimiter are grouped into a single category and defined as the Common Prefix.
// Then, all Common Prefixes are listed. If no Prefix is present, the process starts from the beginning of the path.
getBucketRequest.setDelimiter("/");

cosXmlService.getBucketAsync(getBucketRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketResult getBucketResult = (GetBucketResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Querying the List of Object Versions

Query some or all objects in a bucket with versioning enabled.

Prerequisites: Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Creating CosXmlService.

Use Case: Obtaining the First Page of Data for the Object Version List

String bucketName = "examplebucket-1250000000"; // Format: BucketName-APPID;
final GetBucketObjectVersionsRequest getBucketRequest =
new GetBucketObjectVersionsRequest(bucketName);

// Prefix matching, used to specify the prefix address of the returned objects.
getBucketRequest.setPrefix("dir/");

// The maximum number of entries returned in a single response, with a default value of 1,000.
getBucketRequest.setMaxKeys(100);

cosXmlService.getBucketObjectVersionsAsync(getBucketRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketObjectVersionsResult getBucketResult =
(GetBucketObjectVersionsResult) result;
if (getBucketResult.listVersionResult.isTruncated) {
// Indicates that data is truncated and the next page of data needs to be fetched.
prevPageResult = getBucketResult;
}
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Obtaining the Next Page of Data for the Object Version List

String bucketName = "examplebucket-1250000000"; // Format: BucketName-APPID;
final GetBucketObjectVersionsRequest getBucketRequest =
new GetBucketObjectVersionsRequest(bucketName);

// Prefix matching, used to specify the prefix address of the returned objects.
getBucketRequest.setPrefix("dir/");

// The maximum number of entries returned in a single response, with a default value of 1,000.
getBucketRequest.setMaxKeys(100);

// prevPageResult is the result returned from the previous page, and the nextMarker and nextVersionIdMarker here
// Indicates the starting position of the next page.
getBucketRequest.setKeyMarker(prevPageResult.listVersionResult
.nextKeyMarker);
getBucketRequest.setVersionIdMarker(prevPageResult.listVersionResult
.nextVersionIdMarker);

cosXmlService.getBucketObjectVersionsAsync(getBucketRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
GetBucketObjectVersionsResult getBucketResult =
(GetBucketObjectVersionsResult) result;
if (getBucketResult.listVersionResult.isTruncated) {
// Indicates that data is truncated and the next page of data needs to be fetched.
prevPageResult = getBucketResult;
}
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

SDK API Reference

For detailed parameters and method descriptions of all SDK interfaces, see SDK API Reference.

API Operations

For the description of the API for listing objects, see the List Objects document.
For the description of the API for listing object versions, see the List Object versions document.

Ajuda e Suporte

Esta página foi útil?

comentários