All Products
Search
Document Center

Object Storage Service:List files (Node.js SDK)

Last Updated:Nov 29, 2025

This topic describes how to list all objects, a specified number of objects, or objects with a specified prefix in a bucket when versioning is enabled.

List information about all objects in a bucket

The following code shows how to list the version information of all objects, including delete markers, in a specified bucket:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// List the version information of all objects, including delete markers.
async function getObjectVersions() {
  let nextKeyMarker = null;
  let nextVersionMarker = null;
  let versionListing = null;
  do {
    versionListing = await client.getBucketVersions({
      keyMarker: nextKeyMarker,
      versionIdMarker: nextVersionMarker,
    });

    versionListing.objects.forEach((o) => {
      console.log(`${o.name}, ${o.versionId}`);
    });
    versionListing.deleteMarker.forEach((o) => {
      console.log(`${o.name}, ${o.versionId}`);
    });

    nextKeyMarker = versionListing.NextKeyMarker;
    nextVersionMarker = versionListing.NextVersionIdMarker;
  } while (versionListing.isTruncated);
}

getObjectVersions();

List version information of objects with a specified prefix

The following code shows how to list the version information of objects that have a specified prefix:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// List the version information of objects that have the "test-" prefix.
async function getObjectVersionsByPrefix() {
  let nextKeyMarker = null;
  let nextVersionMarker = null;
  let versionListing = null;
  const prefix = 'test-'
  do {
    versionListing = await client.getBucketVersions({
      keyMarker: nextKeyMarker,
      versionIdMarker: nextVersionMarker,
      prefix
    })
    versionListing.objects.forEach(o => {
      console.log(`${o.name}, ${o.versionId}`)
    })
    versionListing.deleteMarker.forEach(o => {
      console.log(`${o.name}, ${o.versionId}`)
    })
    nextKeyMarker = versionListing.NextKeyMarker;
    nextVersionMarker = versionListing.NextVersionIdMarker;
  } while (versionListing.isTruncated);
}

getObjectVersionsByPrefix();

List version information of a specified number of objects

The following code shows how to list the version information of a specified number of objects:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

async function getObjectVersionByNumber() {
  // List the version information of a maximum of 100 objects.
  const versionListing = await client.getBucketVersions({
    "max-keys": 100,
  });
  // Obtain the version information of the objects. If versioning is disabled, the VersionId is "none".
  versionListing.objects.forEach((o) => {
    console.log(`${o.name}, ${o.versionId}`);
  });
  versionListing.deleteMarker.forEach((o) => {
    console.log(`${o.name}, ${o.versionId}`);
  });
}

getObjectVersionByNumber();

Folder feature

OSS does not have a native folder concept. Instead, all elements are stored as objects. You can simulate a folder by creating a 0 KB object whose name ends with a forward slash (/). This object can be uploaded and downloaded. The OSS console displays objects whose names end with a forward slash (/) as folders.

You can use the `delimiter` and `prefix` parameters to simulate folder functionality:

  • If you set the `prefix` parameter to a folder name, OSS lists all objects whose names start with the specified prefix. This returns all objects in the folder and its subdirectories.

  • If you set the `prefix` parameter and set the `delimiter` parameter to a forward slash (/), OSS lists only the objects and subdirectories at the top level of that folder. The subdirectories are returned in the `CommonPrefixes` element, and objects within those subdirectories are not listed.

Assume a bucket contains four objects: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi4. The forward slash (/) is used as the folder separator. The following examples show how to list objects by simulating folders.

List version information of objects in the root directory

The following code shows how to list the version information of objects in the root directory:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// Set the delimiter parameter to a forward slash (/) to list the version information of objects and the names of folders in the root directory.
async function getRootObjectVersions() {
  let nextKeyMarker = null;
  let nextVersionMarker = null;
  let versionListing = null;
  do {
    versionListing = await client.getBucketVersions({
      keyMarker: nextKeyMarker,
      versionIdMarker: nextVersionMarker,
      delimiter: "/",
    });
    nextKeyMarker = versionListing.NextKeyMarker;
    nextVersionMarker = versionListing.NextVersionIdMarker;
    console.log(versionListing);
  } while (versionListing.isTruncated);
}

getRootObjectVersions();

List files and subdirectories in a directory

The following code shows how to list the objects and subdirectories in a specified directory:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// Set the prefix parameter to get all files and folders in the foo/ directory. Also, set the delimiter parameter to a forward slash (/) to act as the folder separator.
async function getObjectVersionsByPrefixAndDirectory() {
  let nextKeyMarker = null;
  let nextVersionMarker = null;
  let versionListing = null;
  let prefix = "foo/";
  do {
    versionListing = await client.getBucketVersions({
      keyMarker: nextKeyMarker,
      versionIdMarker: nextVersionMarker,
      prefix,
      delimiter: "/",
    });
    nextKeyMarker = versionListing.NextKeyMarker;
    nextVersionMarker = versionListing.NextVersionIdMarker;
    console.log(versionListing);
  } while (versionListing.isTruncated);
}

getObjectVersionsByPrefixAndDirectory();

References

For more information about the API operation for listing objects, see ListObjectVersions (GetBucketVersions).