This topic describes how to list all objects in a bucket, a specified number of objects in a bucket, and objects whose names contain a specified prefix in a bucket.

Simple list

You can call the listObjects or listObjectsV2 operation to list objects in the root directory of a specified bucket. Directories in the root directory and objects in the directories are not listed.

  • List objects by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects in the root directory of a bucket named examplebucket without listing directories in the root directory and objects in the directories. By default, 100 objects are listed at a time.

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // List objects. If you do not specify a prefix, all objects in the root directory are listed. If you specify a prefix, the objects whose names contain the specified prefix are listed. 
    $prefix = 'dir/';
    $options = array(
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    // Query the listed objects. 
    $objectList = $listObjectInfo->getObjectList();
    if (!empty($objectList)) {
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
    }
  • List objects by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects in the root directory of a bucket named examplebucket without listing directories in the root directory and objects in the directories. By default, 100 objects are listed at a time.

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // List objects. If you do not specify a prefix, all objects in the root directory are listed. If you specify a prefix, the objects whose names contain the specified prefix are listed. 
    $prefix = 'dir/';
    $options = array(
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }

List a specified number of objects in a bucket

You can call the listObjects or listObjectsV2 operation to list a specified number of objects in a specified bucket.

  • List a specified number of objects by calling listObjects

    The following code provides an example on how to call the listObjects operation to list 200 objects in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify that up to 200 objects can be listed at a time. 
    $maxkeys = 200;
    $options = array(
      'max-keys' => $maxkeys,
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList(); 
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }
  • List a specified number of objects by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list 200 objects in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify that up to 200 objects can be listed at a time. 
    $maxkeys = 200;
    $options = array(
      'max-keys' => $maxkeys,
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList(); 
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }

List objects whose names contain a specified prefix in a bucket

You can call the listObjects or listObjectsV2 operation to list objects whose names contain a specified prefix in a bucket.

  • List the objects by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects whose names contain the dir prefix in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the prefix to dir. 
    $prefix = 'dir';
    $options = array(
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }
  • List the objects by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects whose names contain the dir prefix in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the prefix to dir. 
    $prefix = 'dir';
    $options = array(
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }

List objects whose names are alphabetically after a specified object

You can call the listObjects or listObjectsV2 operation to list objects whose names are alphabetically after a specified object in a bucket.

  • List the objects by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects whose names are alphabetically after the test.txt object in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify the marker parameter. Example: test.txt. Objects whose names are alphabetically after the object specified by marker are listed. 
    $marker = "test.txt";
    $options = array(
      OssClient::OSS_MARKER=>$marker
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList(); 
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }
  • List the objects by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects whose names are alphabetically after the test.txt object in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify the startAfter parameter. Example: test.txt. Objects whose names are alphabetically after the object specified by startAfter are listed. 
    $startAfter = "test.txt";
    $options = array(
      OssClient::OSS_START_AFTER=>$startAfter
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf($e->getMessage() . "\n");
        return;
    }
    
    $objectList = $listObjectInfo->getObjectList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
          print($objectInfo->getKey() . "\n");
      }
    }

List all objects by page

You can call the listObjects or listObjectsV2 operation to list all objects in a bucket by page. You can configure maxKeys to specify the maximum number of objects that can be listed on each page.

  • List all objects on multiple pages by calling listObjects

    The following code provides an example on how to call the listObjects operation to list all objects in examplebucket by page:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
        // Set maxKeys to 200. 
        OssClient::OSS_MAX_KEYS=>200
    );
    do{
        $result = $ossClient->listObjects($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
        $options[OssClient::OSS_MARKER] = $result->getNextMarker();
    }while($result->getIsTruncated() === 'true');
  • List all objects on multiple pages by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list all objects in examplebucket by page:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
        // Set maxKeys to 200. 
        OssClient::OSS_MAX_KEYS=>200
    );
    do{
        $result = $ossClient->listObjectsV2($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
        $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken();
    }while($result->getIsTruncated() === 'true');

List objects whose names contain a specified prefix by page

You can call the listObjects or listObjectsV2 operation to list objects whose names contain a specified prefix in a bucket by page.

  • List the objects on multiple pages by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects whose names contain the dir prefix in examplebucket by page:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify that up to 100 objects can be listed on each page. 
    $maxKeys = 100;
    // Specify the prefix. Example: dir. 
    $prefix = 'dir';
    $options = array(
        OssClient::OSS_MAX_KEYS =>$maxKeys,
        OssClient::OSS_PREFIX =>$prefix
    );
    do{
        $result = $ossClient->listObjects($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
        $options[OssClient::OSS_MARKER] = $result->getNextMarker();
    }while($result->getIsTruncated() === 'true');
  • List the objects on multiple pages by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects whose names contain the dir prefix in examplebucket by page:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Specify that up to 100 objects can be listed on each page. 
    $maxKeys = 100;
    // Specify the prefix. Example: dir. 
    $prefix = 'dir';
    $options = array(
        OssClient::OSS_MAX_KEYS =>$maxKeys,
        OssClient::OSS_PREFIX =>$prefix
    );
    do{
        $result = $ossClient->listObjectsV2($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
        $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken();
    }while($result->getIsTruncated() === 'true');

List objects and specify the encoding type of the object names

An object name that contains one of the following special characters must be encoded before the object is transmitted. OSS supports only URL encoding.

  • Single quotation marks (')
  • Double quotations marks (")
  • Ampersands (&)
  • Angle brackets (<>)
  • Pause markers (、)
  • Chinese characters

You can call the listObjects or listObjectsV2 operation to list objects and specify the encoding type of the object names.

  • List objects and specify the encoding type of the object names by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects and specify the encoding type of the object names:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
        // Specify that the names of the listed objects are URL-encoded. 
        OssClient::OSS_ENCODING_TYPE=>'url',
        // Specify that up to 20 objects can be listed on each page. 
        OssClient::OSS_MAX_KEYS=>20
    );
    do{
        $result = $ossClient->listObjects($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
    
        print("prefix:\n");
        $prefixList = $result->getPrefixList();
        foreach ($prefixList as $prefixInfo) {
            print($prefixInfo->getPrefix() . "\n");
        }
    
        if($result->getDelimiter() != null){
            printf("delimiter:".$result->getDelimiter().PHP_EOL);
        }
    
        if($result->getMarker() != null){
            printf("marker:".$result->getMarker().PHP_EOL);
        }
    
         $options[OssClient::OSS_MARKER] = $result->getNextMarker();
    }while($result->getIsTruncated() === 'true');
  • List objects and specify the encoding type of the object names by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects and specify the encoding type of the object names:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
        // Specify that the object names are URL-encoded. 
        OssClient::OSS_ENCODING_TYPE=>'url',
        // Specify that up to 20 objects can be listed on each page. 
        OssClient::OSS_MAX_KEYS=>20
    );
    do{
        $result = $ossClient->listObjectsV2($bucket,$options);
        $objectList = $result->getObjectList();
        print("objectList:\n");
        foreach ($objectList as $objectInfo) {
            print($objectInfo->getKey() . "\n");
        }
    
        print("prefix:\n");
        $prefixList = $result->getPrefixList();
        foreach ($prefixList as $prefixInfo) {
            print($prefixInfo->getPrefix() . "\n");
        }
    
        if($result->getDelimiter() != null){
            printf("delimiter:".$result->getDelimiter().PHP_EOL);
        }
    
        if($result->getStartAfter() != null){
            printf("start after:".$result->getStartAfter().PHP_EOL);
        }
    
        $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken();
    }while($result->getIsTruncated() === 'true');

List objects by directory

OSS uses a flat structure for objects instead of a hierarchical structure to store objects. A directory is an object whose size is 0 and whose name ends with a forward slash (/). You can upload and download this object. By default, objects whose names end with a forward slash (/) are displayed as directories in the OSS console.

You can specify the delimiter and prefix parameters to list objects by directory.

  • If you set prefix to a directory name in the request, objects and subdirectories whose names contain the prefix are listed.
  • If you also set delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in commonPrefixes. The objects and directories in these subdirectories are not listed.

Example: The following four objects are stored in a bucket: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is specified as the directory delimiter. The following code provides examples on how to list objects by directory.

List all objects in a bucket

You can call the listObjects or listObjectsV2 operation to list all objects in a specified bucket.

  • List all objects in a bucket by calling listObjects

    The following code provides an example on how to call the listObjects operation to list all objects in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
      'delimiter' => '',
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket,$options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • List all objects in a bucket by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list all objects in examplebucket:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    $options = array(
      'delimiter' => '',
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket,$options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • Returned result

    The following result is returned when you call the preceding operations to list all objects in examplebucket:

    objectList:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg

List all objects in a specified directory

You can call the listObjects or listObjectsV2 operation to list all objects in the fun/ directory.

  • List all objects in a specified directory by calling listObjects

    The following code provides an example on how to call the listObjects operation to list all objects in the fun/ directory:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $options = array(
      'prefix' => $prefix,
      'delimiter' => '',
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • List all objects in a specified directory by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list all objects in the fun/ directory:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $options = array(
      'prefix' => $prefix,
      'delimiter' => '',
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • Returned result

    The following result is returned when you call the preceding two operations to list all objects in the fun/ directory:

    objectList:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg

List objects and subdirectories in a specified directory

You can call the listObjects or listObjectsV2 operation to list objects and subdirectories in the fun/ directory.

  • List objects and subdirectories in a specified directory by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects and subdirectories in the fun/ directory:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $delimiter = '/'; 
    $options = array(
      'delimiter' => $delimiter,
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjects($bucket, $options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    // CommonPrefixs lists all subdirectories in the fun/ directory. 
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • List objects and subdirectories in a specified directory by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects and subdirectories in the fun/ directory:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $delimiter = '/';
    $options = array(
      'delimiter' => $delimiter,
      'prefix' => $prefix,
    );
    try {
      $listObjectInfo = $ossClient->listObjectsV2($bucket, $options);
    } catch (OssException $e) {
      printf(__FUNCTION__ . ": FAILED\n");
      printf($e->getMessage() . "\n");
      return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
      print("objectList:\n");
      foreach ($objectList as $objectInfo) {
        print($objectInfo->getKey() . "\n");
      }
    }
    // CommonPrefixs lists all subdirectories in the fun/ directory. 
    if (!empty($prefixList)) {
      print("prefixList: \n");
      foreach ($prefixList as $prefixInfo) {
        print($prefixInfo->getPrefix() . "\n");
      }
    }
  • Returned result

    The following result is returned when you call the preceding two operations to list objects and subdirectories in the fun/ directory:

    objectList:
    fun/test.jpg
    prefixList:
    fun/movie/

List objects in a specified directory and query the size of each listed object

You can call the listObjects or listObjectsV2 operation to list objects in the fun/ directory and query the size of each listed object.

  • List objects in a specified directory and query the size of each listed object by calling listObjects

    The following code provides an example on how to call the listObjects operation to list objects in the fun/ directory and query the size of each listed object:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $delimiter = '';
    $nextMarker = '';
    $maxkeys = 1000;
    $options = array(
        'delimiter' => $delimiter,
        'prefix' => $prefix,
        'max-keys' => $maxkeys,
        'marker' => $nextMarker,
    );
    $bool = true;
    $size = 0;
    while ($bool){
        $result = $ossClient->listObjects($bucket,$options);
        foreach ($result->getObjectList() as $objInfo){
            printf("object name".$objInfo->getKey().":" . ($objInfo->getSize() / 1024) . "KB".PHP_EOL);
            $size+=$objInfo->getSize();
        }
        if($result->getIsTruncated() === 'true'){
            $options['marker'] = $result->getNextMarker();
        }else{
            $bool = false;
        }
    }
    printf($prefix.":" . ($size / 1024) . "KB".PHP_EOL);
  • List objects in a specified directory and query the size of each listed object by calling listObjectsV2

    The following code provides an example on how to call the listObjectsV2 operation to list objects in the fun/ directory and query the size of each listed object:

    <?php
    if (is_file(__DIR__ . '/../autoload.php')) {
        require_once __DIR__ . '/../autoload.php';
    }
    if (is_file(__DIR__ . '/../vendor/autoload.php')) {
        require_once __DIR__ . '/../vendor/autoload.php';
    }
    require_once __DIR__ . '/Common.php';
    use OSS\OssClient;
    use OSS\Core\OssException;
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    $accessKeyId = "yourAccessKeyId";
    $accessKeySecret = "yourAccessKeySecret";
    // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    $endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // Specify the bucket name. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    // Set the directory name to fun/. 
    $prefix = 'fun/';
    $delimiter = '';
    $nextMarker = '';
    $maxkeys = 1000;
    $options = array(
        'delimiter' => $delimiter,
        'prefix' => $prefix,
        'max-keys' => $maxkeys,
    );
    $bool = true;
    $size = 0;
    while ($bool){
        $result = $ossClient->listObjectsV2($bucket,$options);
        foreach ($result->getObjectList() as $objInfo){
            printf("object name".$objInfo->getKey().":" . ($objInfo->getSize() / 1024) . "KB".PHP_EOL);
            $size+=$objInfo->getSize();
        }
        if($result->getIsTruncated() === 'true'){
            $options[OssClient::OSS_CONTINUATION_TOKEN] = $result->getNextContinuationToken();
        }else{
            $bool = false;
        }
    }
    printf($prefix.":" . ($size / 1024) . "KB".PHP_EOL);
  • Returned result

    The following result is returned when you call the preceding operations to list objects in the fun/ directory and query the size of each listed object:

    object namefun/movie/001.avi:0.01953125KB
    object namefun/movie/007.avi:290.71875KB
    object namefun/test.jpg:144.216796875KB
    fun/:434.955078125KB

References