View the object in the bucket
Simple query
View the list of objects in a bucket.
Basic workflow
- Instantiate the BOSClient class.
- Calling the BOSClient.listObjects(bucketName) method returns an instance of the ListObjectsResponse class.
- For the ListObjectsResponse class, methods like getBuckets(), getOwner(), and getMetadata() can be executed.
Example code
1// Obtain all object information under the specified bucket
2ListObjectsResponse listing = client.listObjects(<BucketName>);
3 // Traverse all objects
4for (BosObjectSummary objectSummary : listing.getContents()) {
5 System.out.println("ObjectKey: " + objectSummary.getKey());
6}
Note: The listObjects() method returns a ListObjectsResponse object, which contains the results of this listObject request. Users can obtain the description information of all objects through the getContents method in ListObjectsResponse.
- By default, if the number of objects in the bucket exceeds 1,000, only 1,000 objects will be returned. In this case, the IsTruncated value in the returned result will be set to True, and NextMarker will be provided as the starting point for the next retrieval.
- To retrieve more objects, use the Marker parameter for reading in batches. Refer to [Extended Query](#Extended query).
Complete example
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.demo.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9import com.baidubce.services.bos.model.BosObjectSummary;
10import com.baidubce.services.bos.model.ListObjectsResponse;
11public class ExampleActivity extends Activity {
12private String bucketName = <BucketName>;
13@Override
14protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17 new Thread(new Runnable() {
18 @Override
19 public void run() {
20 try {
21 BosClientConfiguration config = new BosClientConfiguration();
22 config.setCredentials(new DefaultBceCredentials(<AccessKeyId>, <SecretAccessKey>));
23 config.setEndpoint(<EndPoint>);
24 BosClient client = new BosClient(config);
25 // Obtain all object information under the specified bucket
26 ListObjectsResponse listing = client.listObjects(<BucketName>);
27 // Traverse all objects
28 for (BosObjectSummary objectSummary : listing.getContents()) {
29 System.out.println("ObjectKey: " + objectSummary.getKey());
30 }
31 } catch (BceServiceException e) {
32 System.out.println("Error ErrorCode: " + e.getErrorCode());
33 System.out.println("Error RequestId: " + e.getRequestId());
34 System.out.println("Error StatusCode: " + e.getStatusCode());
35 System.out.println("Error Message: " + e.getMessage());
36 System.out.println("Error ErrorType: " + e.getErrorType());
37 } catch (BceClientException e) {
38 System.out.println("Error Message: " + e.getMessage());
39 }
40 }
41 }).start();
42}}
Extended query
Users can configure the ListObjectsRequest parameters to perform advanced query operations. The extended parameters available for configuration in ListObjectsRequest include the following:
| Parameter name | Description | Default value |
|---|---|---|
| MaxKeys | Specify the maximum number of objects to return, which must not exceed 1,000. | 1000 |
| Prefix | Set the prefix of the objectKey. The prefix means that the objectKey contains and starts with the value of Prefix. It is usually used in conjunction with Delimiter when [querying simulated folders](#Query simulated folders). |
- |
| Delimiter | It is a delimiter used to hierarchize objectKey. It is usually used in conjunction with Prefix when [querying simulated folders](#Query simulated folders). The objectKey from the Prefix to the first occurrence of the Delimiter character is called: CommonPrefixes. |
- |
| Marker | It is a string used to set the starting position of the returned results. After setting the Marker value, the returned objects will be returned starting from the Marker value in alphabetical order. |
- |
Basic workflow
- Create an instance of the ListObjectsRequest class.
- Use methods like setDelimiter(), setMarker(), or setPrefix() in ListObjectsRequest to enable more advanced query functionalities.
- Create an instance of the BOSClient class and invoke the listObjects(listObjectsRequest) method.
Example code
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
3 // Set extended query parameters
4listObjectsRequest.setDelimiter(<Delimiter>);
5listObjectsRequest.setMarker(<Marker>);
6...
7ListObjectsResponse listing = client.listObjects(listObjectsRequest);
Note: The above code calls an overloaded method from
listObjectsby passing aListObjectsRequestto complete the request.
Complete example
Example 1:
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.demo.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9import com.baidubce.services.bos.model.BosObjectSummary;
10import com.baidubce.services.bos.model.ListObjectsRequest;
11import com.baidubce.services.bos.model.ListObjectsResponse;
12public class ExampleActivity extends Activity {
13private String bucketName = <BucketName>;
14@Override
15protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18 new Thread(new Runnable() {
19 @Override
20 public void run() {
21 try {
22 BosClientConfiguration config = new BosClientConfiguration();
23 config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));
24 config.setEndpoint(<EndPoint>);
25 BosClient client = new BosClient(config);
26 // Construct a ListObjectsRequest request
27 ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
28 // Set parameters
29 listObjectsRequest.setDelimiter("/");
30 listObjectsRequest.setMarker("123");
31 listObjectsRequest.setMaxKeys(100);
32 listObjectsRequest.setPrefix("fun");
33 // Obtain all eligible object information under the specified bucket
34 ListObjectsResponse listing = client.listObjects(listObjectsRequest);
35 // Traverse all objects
36 for (BosObjectSummary objectSummary : listing.getContents()) {
37 System.out.println("ObjectKey: " + objectSummary.getKey());
38 }
39 } catch (BceServiceException e) {
40 System.out.println("Error ErrorCode: " + e.getErrorCode());
41 System.out.println("Error RequestId: " + e.getRequestId());
42 System.out.println("Error StatusCode: " + e.getStatusCode());
43 System.out.println("Error Message: " + e.getMessage());
44 System.out.println("Error ErrorType: " + e.getErrorType());
45 } catch (BceClientException e) {
46 System.out.println("Error Message: " + e.getMessage());
47 }
48 }
49 }).start();
50}}
Example II: A complete example using the NextMarker.
1import android.app.Activity;
2import android.os.Bundle;
3import com.baidubce.BceClientException;
4import com.baidubce.BceServiceException;
5import com.baidubce.auth.DefaultBceCredentials;
6import com.baidubce.demo.R;
7import com.baidubce.services.bos.BosClient;
8import com.baidubce.services.bos.BosClientConfiguration;
9import com.baidubce.services.bos.model.BosObjectSummary;
10import com.baidubce.services.bos.model.ListObjectsRequest;
11import com.baidubce.services.bos.model.ListObjectsResponse;
12public class ExampleActivity extends Activity {
13private String bucketName = <BucketName>;
14@Override
15protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.activity_main);
18 new Thread(new Runnable() {
19 @Override
20 public void run() {
21 try {
22 BosClientConfiguration config = new BosClientConfiguration();
23 config.setCredentials(new DefaultBceCredentials(<AccessKeyId>, <SecretAccessKey>);
24 config.setEndpoint(<EndPoint>);
25 BosClient client = new BosClient(config);
26 ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
27 listObjectsRequest.setMarker("");
28 listObjectsRequest.setMaxKeys(20);
29 listObjectsRequest.setDelimiter(BceConfig.BOS_DELIMITER);
30 ListObjectsResponse listObjectsResponse = client.listObjects(listObjectsRequest);
31 String nextMark = "";
32 if(listObjectsResponse.isTruncated()){
33 nextMark = listObjectsResponse.getNextMarker();
34 }
35 ListObjectsRequest listObjectsRequest2 = new ListObjectsRequest(<BucketName>);
36 listObjectsRequest2.setMarker(nextMark);
37 listObjectsRequest2.setMaxKeys(20);
38 listObjectsRequest2.setDelimiter(BceConfig.BOS_DELIMITER);
39 ListObjectsResponse listing = client.listObjects(listObjectsRequest2);
40 for (BosObjectSummary objectSummary : listing.getContents()) {
41 System.out.println("ObjectKey: " + objectSummary.getKey());
42 }
43 } catch (BceServiceException e) {
44 System.out.println("Error ErrorCode: " + e.getErrorCode());
45 System.out.println("Error RequestId: " + e.getRequestId());
46 System.out.println("Error StatusCode: " + e.getStatusCode());
47 System.out.println("Error Message: " + e.getMessage());
48 System.out.println("Error ErrorType: " + e.getErrorType());
49 } catch (BceClientException e) {
50 System.out.println("Error Message: " + e.getMessage());
51 }
52 }
53 }).start();
54}}
Query simulated folders
Since BOS is inherently a (<Key>,<Value>) storage system, the concept of "folder" does not exist in principle. However, you can simulate the folder function by combining the Delimiter and Prefix} parameters.
Suppose the bucket contains five files: bos.jpg, fun/, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The "/" symbol can be used as a delimiter to mimic folder structures.
Recursively list all files under the simulated folder
You can obtain all files under a simulated folder by setting the Prefix parameter:
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
3 // Recursively list all files under the fun directory
4listObjectsRequest.setPrefix("fun/");
5ListObjectsResponse listing = client.listObjects(listObjectsRequest);
6 // Traverse all objects
7System.out.println("Objects:");
8for (BosObjectSummary objectSummary : listing.getContents()) {
9 System.out.println(objectSummary.getKey());
10}
Output:
1Objects:
2fun/
3fun/movie/001.avi
4fun/movie/007.avi
5fun/test.jpg
View files and subfolders under the simulated folder
With the combination of Prefix and Delimiter, it can list files and subfolders under the simulated folder:
1// Construct a ListObjectsRequest request
2ListObjectsRequest listObjectsRequest = new ListObjectsRequest(<BucketName>);
3 // Specify "/" as the delimiter for simulated folder
4listObjectsRequest.setDelimiter("/");
5 // List all files and subfolders under the “fun” folder
6listObjectsRequest.setPrefix("fun/");
7ListObjectsResponse listing = client.listObjects(listObjectsRequest);
8 // Traverse all objects
9System.out.println("Objects:");
10for (BosObjectSummary objectSummary : listing.getContents()) {
11 System.out.println(objectSummary.getKey());
12}
13 // Traverse all CommonPrefix
14System.out.println("\nCommonPrefixs:");
15for (String commonPrefix : listing.getCommonPrefixes()) {
16 System.out.println(commonPrefix);
17}
Output:
1Objects:
2fun/
3fun/test.jpg
4CommonPrefixs:
5fun/movie/
Description:
In the returned results, the list under
Objectsshows the files under the fun folder. The list inCommonPrefixsshows all subfolders under the fun folder. It can be seen that the two filesfun/movie/001.aviandfun/movie/007.aviare not listed because they belong to themoviesubfolder under thefunfolder.
