View the object in the bucket
View the object in the bucket.
Simple query
View the list of objects in a bucket.
-
Basic workflow
- Create a BosClient instance.
- Run the listObjects() method.
-
Example code
JavaScript1client.listObjects(<bucketName>) 2 .then(function (response) { 3 var contents = response.body.contents; 4 for (var i = 0, l = contents.length; i < l; i++) { 5 console.log(contents[i].key); 6 } 7 }) 8 .catch(function (error) { 9 // Query failed 10 });Plain Text1> **Note:** 2> 3> - 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. 4> 5> - To retrieve more objects, use the Marker parameter for reading in batches. Refer to [Extended Query](#Extended query).
Extended query
Users can configure listObjects parameters to activate additional extended query features. The configurable extended parameters are as follows:
| 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. |
- |
-
Example code
JavaScript1// Set parameters 2var options = { 3 delimiter: '/', 4 marker: '123' 5}; 6client.listObjects(<bucketName>, options) 7 .then(function (response) { 8 var contents = response.body.contents; 9 for (var i = 0, l = contents.length; i < l; i++) { 10 console.log(contents[i].key); 11 } 12 }) 13 .catch(function (error) { 14 // Query failed 15 });
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// Set parameters
2let options = {
3 prefix: 'fun/' // Recursively list all files under the fun directory
4};
5client.listObjects(<bucketName>, options)
6.then(function (response) {
7 console.log('Objects:');
8 var contents = response.body.contents;
9 for (var i = 0, l = contents.length; i < l; i++) {
10 console.log(contents[i].key);
11 }
12})
13.catch(function (error) {
14 // Query failed
15});
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// Set parameters
2let options = {
3 prefix: 'fun/',//List all files and folders under the “fun” directory
4 delimiter: '/' // "/" is the delimiter for folders
5};
6client.listObjects(<bucketName>, options)
7.then(function (response) {
8 console.log('Objects:');
9 var contents = response.body.contents;
10 for (var i = 0, l = contents.length; i < l; i++) {
11 console.log(contents[i].key);
12 }
13 console.log('CommonPrefixs:');
14 var commonPrefixes = response.body.commonPrefixes;
15 for (i = 0, l = commonPrefixes.length; i < l; i++) {
16 console.log(commonPrefixes[i]);
17 }
18})
19.catch(function (error) {
20 // Query failed
21});
Output:
1Objects:
2fun/
3fun/test.jpg
4CommonPrefixs:
5fun/movie/
Note: In the returned results, the list of
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.
