All Products
Search
Document Center

Object Storage Service:List objects by using OSS SDK for PHP

Last Updated:Nov 07, 2024

This topic describes how to list all objects, a specific number of objects, and objects whose names contain a specific prefix in an Object Storage Service (OSS) bucket.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To list objects, you must have the oss:ListObjects permission. For more information, see Attach a custom policy to a RAM user.

List objects by using simple list

You can call the listObjects or listObjectsV2 operation to list objects in the root directory of a specific bucket. Subdirectories in the root directory and objects in the subdirectories are not listed.

  • List objects by calling the listObjects operation

    The following sample 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 subdirectories in the root directory and objects in the subdirectories. 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    try {
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        $provider = new EnvironmentVariableCredentialsProvider();
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // Specify the name of the bucket. Example: examplebucket. 
        $bucket= "examplebucket";
        $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint, 
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"       
        );
        $ossClient = new OssClient($config);
        $listObjectInfo = $ossClient->listObjects($bucket);
        printf("Bucket Name: %s". "\n",$listObjectInfo->getBucketName());
        printf("Prefix: %s". "\n",$listObjectInfo->getPrefix());
        printf("Marker: %s". "\n",$listObjectInfo->getMarker());
        printf("Next Marker: %s". "\n",$listObjectInfo->getNextMarker());
        printf("Max Keys: %s". "\n",$listObjectInfo->getMaxKeys());
        printf("Delimiter: %s". "\n",$listObjectInfo->getDelimiter());
        printf("Is Truncated: %s". "\n",$listObjectInfo->getIsTruncated());
        $objectList = $listObjectInfo->getObjectList();
        $prefixList = $listObjectInfo->getPrefixList();
        if (!empty($objectList)) {
            print("objectList:\n");
            foreach ($objectList as $objectInfo) {
                printf("Object Name: %s". "\n",$objectInfo->getKey());
                printf("Object Size: %s". "\n",$objectInfo->getSize());
                printf("Object Type: %s". "\n",$objectInfo->getType());
                printf("Object ETag: %s". "\n",$objectInfo->getETag());
                printf("Object Last Modified: %s". "\n",$objectInfo->getLastModified());
                printf("Object Storage Class: %s". "\n",$objectInfo->getStorageClass());
    
                if ($objectInfo->getRestoreInfo()){
                    printf("Restore Info: %s". "\n",$objectInfo->getRestoreInfo() );
                }
    
                if($objectInfo->getOwner()){
                    printf("Owner Id:".$objectInfo->getOwner()->getId() . "\n");
                    printf("Owner Name:".$objectInfo->getOwner()->getDisplayName() . "\n");
                }
            }
        }
        if (!empty($prefixList)) {
            print("prefixList: \n");
            foreach ($prefixList as $prefixInfo) {
                printf("Common Prefix:%s\n",$prefixInfo->getPrefix());
            }
        }
    } catch (OssException $e) {
        printf($e->getMessage() . "\n");
        return;
    }
    
  • List objects by calling the listObjectsV2 operation

    The following sample 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 subdirectories in the root directory and objects in the subdirectories. 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\Core\OssException;
    
    try {
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        $provider = new EnvironmentVariableCredentialsProvider();
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        // Specify the name of the bucket. Example: examplebucket. 
        $bucket= "examplebucket";
        $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint, 
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"       
        );
        $ossClient = new OssClient($config);
        $listObjectInfo = $ossClient->listObjectsV2($bucket);
        printf("Bucket Name: %s". "\n",$listObjectInfo->getBucketName());
        printf("Prefix: %s". "\n",$listObjectInfo->getPrefix());
        printf("Next Continuation Token: %s". "\n",$listObjectInfo->getNextContinuationToken());
        printf("Continuation Token: %s". "\n",$listObjectInfo->getContinuationToken());
        printf("Max Keys: %s". "\n",$listObjectInfo->getMaxKeys());
        printf("Key Count: %s". "\n",$listObjectInfo->getKeyCount());
        printf("Delimiter: %s". "\n",$listObjectInfo->getDelimiter());
        printf("Is Truncated: %s". "\n",$listObjectInfo->getIsTruncated());
        printf("Start After: %s". "\n",$listObjectInfo->getStartAfter());
        $objectList = $listObjectInfo->getObjectList(); 
        $prefixList = $listObjectInfo->getPrefixList();
        if (!empty($objectList)) {
            print("objectList:\n");
            foreach ($objectList as $objectInfo) {
                printf("Object Name: %s". "\n",$objectInfo->getKey());
                printf("Object Size: %s". "\n",$objectInfo->getSize());
                printf("Object Type: %s". "\n",$objectInfo->getType());
                printf("Object ETag: %s". "\n",$objectInfo->getETag());
                printf("Object Last Modified: %s". "\n",$objectInfo->getLastModified());
                printf("Object Storage Class: %s". "\n",$objectInfo->getStorageClass());
    
                if ($objectInfo->getRestoreInfo()){
                    printf("Restore Info: %s". "\n",$objectInfo->getRestoreInfo() );
                }
    
                if($objectInfo->getOwner()){
                    printf("Owner Id:".$objectInfo->getOwner()->getId() . "\n");
                    printf("Owner Name:".$objectInfo->getOwner()->getDisplayName() . "\n");
                }
            }
        }
        if (!empty($prefixList)) {
            print("prefixList: \n");
            foreach ($prefixList as $prefixInfo) {
                printf("Common Prefix:%s\n",$prefixInfo->getPrefix());
            }
        }
    } catch (OssException $e) {
        printf($e->getMessage() . "\n");
        return;
    }
    
    

List a specific number of objects

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

  • List a specific number of objects by calling the listObjects operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 specific number of objects by calling the listObjectsV2 operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 the specified prefix

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

  • List objects whose names contain a specific prefix by calling the listObjects operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 objects whose names contain a specific prefix by calling the listObjectsV2 operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 specific object

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

  • List objects whose names are alphabetically after a specific object by calling the listObjects operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // Specify the marker parameter. Example: test.txt. Objects whose names are alphabetically after the object specified by the marker parameter 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 objects whose names are alphabetically after a specific object by calling the listObjectsV2 operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // Specify the startAfter parameter. Example: test.txt. Objects whose names are alphabetically after the object specified by the startAfter parameter 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 the maxKeys parameter to specify the maximum number of objects that can be listed on each page.

  • List all objects by page by calling the listObjects operation

    The following sample code provides an example on how to call the listObjects operation to list all objects in the examplebucket bucket 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $options = array(
        // Specify that up to 200 objects are listed on each page. 
        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 by page by calling the listObjectsV2 operation

    The following sample code provides an example on how to call the listObjectsV2 operation to list all objects in the examplebucket bucket 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $options = array(
        // Specify that up to 200 objects are listed on each page. 
        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 specific prefix by page

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

  • List objects whose names contain a specific prefix by page by calling the listObjects operation

    The following sample code provides an example on how to call the listObjects operation to list objects whose names contain the dir prefix in the examplebucket bucket 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 objects whose names contain a specific prefix by page by calling the listObjectsV2 operation

    The following sample code provides an example on how to call the listObjectsV2 operation to list objects whose names contain the dir prefix in the examplebucket bucket 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 whose names are encoded by using a specific encoding type

If the name of an object contains one of the following characters, the object name must be URL-encoded before the object can be transmitted:

  • Single quotation marks (')

  • Double quotations marks (")

  • Ampersands (&)

  • Angle brackets (<>)

  • Pause markers (、)

  • Chinese characters

You can call the listObjects or listObjectsV2 operation to list objects whose names are encoded by using a specific encoding type.

  • List objects whose names are encoded by using a specific encoding type by calling the listObjects operation

    The following sample code provides an example on how to call the listObjects operation to list objects whose names are encoded by using a specific encoding type:

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $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->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 whose names are encoded by using a specific encoding type by calling the listObjectsV2 operation

    The following sample code provides an example on how to call the listObjectsV2 operation to list objects whose names are encoded by using a specific encoding type:

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $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 to store objects. A directory is a zero-byte object whose name ends with a forward slash (/). You can upload and download this directory. 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, the objects and subdirectories whose names contain the prefix are listed.

  • If you specify a prefix and 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.

For example, a bucket contains the following objects: 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 examples describe how to list objects in simulated directories.

List all objects in a bucket

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

  • List all objects in a bucket by calling the listObjects operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $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 the listObjectsV2 operation

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

    <?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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    $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");
      }
    }
  • Sample response

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

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

List all objects in a specific directory

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

  • List all objects in a specific directory by calling the listObjects operation

    The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 specific directory by calling the listObjectsV2 operation

    The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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");
      }
    }
  • Sample response

    The following response 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 specific directory

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

  • List objects and subdirectories in a specific directory by calling the listObjects operation

    The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
     $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 specific directory by calling the listObjectsV2 operation

    The following sample 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
     $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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");
      }
    }
  • Sample response

    The following response 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 the sizes of objects in a specific directory

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

  • List the sizes of objects in a specific directory by calling the listObjects operation

    The following sample code provides an example on how to call the listObjects operation to list the sizes of 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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 the sizes of objects in a specific directory by calling the listObjectsV2 operation

    The following sample code provides an example on how to call the listObjectsV2 operation to list the sizes of 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\Credentials\EnvironmentVariableCredentialsProvider;
    use OSS\OssClient;
    use OSS\CoreOssException;
    
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.  
    $provider = new EnvironmentVariableCredentialsProvider();
    // 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 name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    // 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);
  • Sample response

    The following response is returned when you call the preceding operations to list the sizes of objects in the fun/ directory:

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

References