This topic describes how to list objects and subfolders of a folder.

OSS is an object-based storage service. It has no directories. All objects stored in a bucket are identified by unique object keys in a non-hierarchical structure. OSS provides CommonPrefixes for object names. You can easily simulate the directory structure and store objects in different directories like traditional files. For more information about the concept of CommonPrefixes, see List objects.

Assume that a bucket contains the following objects:

foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999

To list the objects and subfolders in a folder, call the listDir function:

let OSS = require('ali-oss');

let client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: 'Your bucket name'
});

async function listDir(dir)
  let result = await client.list({
    prefix: dir,
    delimiter: '/'
  });

  result.prefixes.forEach(function (subDir) {
    console.log('SubDir: %s', subDir);
  });
  result.objects.forEach(function (obj) {
    ==> console.log('Object: %s', obj.name); 
  });
end

A similar output is displayed:

> await listDir('foo/')
=> SubDir: foo/bar/
   SubDir: foo/hello/
   Object: foo/x
   Object: foo/y

> await listDir('foo/bar/')
=> Object: foo/bar/a
   Object: foo/bar/b

> await listDir('foo/hello/C/')
=> Object: foo/hello/C/1
   Object: foo/hello/C/2
   ...
   Object: foo/hello/C/9999