All Products
Search
Document Center

Object Storage Service:List objects (PHP SDK V1)

Last Updated:Nov 29, 2025

This topic describes how to list files (objects) in a bucket for which versioning is enabled. You can list all files, a specified number of files, or files with a specified prefix.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • 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.

  • You must have the oss:ListObjectVersions permission to list files. For more information, see Grant custom permissions to a RAM user.

List all object versions in a bucket

The following code provides an example of how to list all object versions, including delete markers, 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 set. 
$provider = new EnvironmentVariableCredentialsProvider();
// Replace yourEndpoint with the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket= "examplebucket";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null
    );
    $bool = true;
    while ($bool){
        $result = $ossClient->listObjectVersions($bucket,$option);
        // View the version information of the objects.
        foreach ($result->getObjectVersionList() as $key => $info){
            printf("key name: {$info->getKey()}\n");
            printf("versionid: {$info->getVersionId()}\n");
            printf("Is latest: {$info->getIsLatest()}\n\n");
        }

        // View the version information of the delete markers.
        foreach ($result->getDeleteMarkerList() as $key => $info){
            printf("del_maker key name: {$info->getKey()}\n");
            printf("del_maker versionid: {$info->getVersionId()}\n");
            printf("del_maker Is latest: {$info->getIsLatest()}\n\n");
        }

        if($result->getIsTruncated() === 'true'){
            $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
        $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
        }else{
            $bool = false;
        }
    }
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

List object versions with a specified prefix

The following code provides an example of how to list the versions of objects with the test 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 set. 
$provider = new EnvironmentVariableCredentialsProvider();
// Replace yourEndpoint with the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket= "examplebucket";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null,
        // List the versions of objects that have the "test" prefix.
        OssClient::OSS_PREFIX => "test"
    );
    $bool = true;
    while ($bool){
        $result = $ossClient->listObjectVersions($bucket,$option);
        // View the version information of the objects.
        foreach ($result->getObjectVersionList() as $key => $info){
            printf("key name: {$info->getKey()}\n");
            printf("versionid: {$info->getVersionId()}\n");
            printf("Is latest: {$info->getIsLatest()}\n\n");
        }

        // View the version information of the delete markers.
        foreach ($result->getDeleteMarkerList() as $key => $info){
            printf("del_maker key name: {$info->getKey()}\n");
            printf("del_maker versionid: {$info->getVersionId()}\n");
            printf("del_maker Is latest: {$info->getIsLatest()}\n");
        }

        if($result->getIsTruncated() === 'true'){
            $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
            $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
        }else{
            $bool = false;
        }
    }
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

List a specified number of object versions

The following code provides an example of how to list a specified number of object versions 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 set. 
$provider = new EnvironmentVariableCredentialsProvider();
// Replace yourEndpoint with the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "yourEndpoint";
// Specify the bucket name. For example, examplebucket.
$bucket= "examplebucket";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null,
        OssClient::OSS_MAX_KEYS => 200   // Specify the maximum number of object versions to list. In this example, a maximum of 200 object versions are listed.
    );
    $result = $ossClient->listObjectVersions($bucket,$option);
    // View the version information of the objects.
    foreach ($result->getObjectVersionList() as $key => $info){
        printf("key name: ".$info->getKey().PHP_EOL);
        printf("versionid: ".$info->getVersionId().PHP_EOL);
        printf("Is latest: ".$info->getIsLatest().PHP_EOL.PHP_EOL);
    }

    // View the version information of the delete markers.
    foreach ($result->getDeleteMarkerList() as $key => $info){
        printf("del_maker key name: ".$info->getKey().PHP_EOL);
        printf("del_maker versionid: ".$info->getVersionId().PHP_EOL);
        printf("del_maker Is latest: ".$info->getIsLatest().PHP_EOL.PHP_EOL);
    }
} catch(OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}

Folder feature

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 object. By default, objects whose names end with a forward slash (/) are displayed as folders in the OSS console.

You can specify the delimiter and prefix parameters in the request 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 specify a prefix and set delimiter to a forward slash (/) in the request, 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.

  • List object versions in the root directory

    The following code provides an example of how to list the versions of objects in the root directory of 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 set. 
    $provider = new EnvironmentVariableCredentialsProvider();
    // Replace yourEndpoint with the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = "yourEndpoint";
    // Specify the bucket name. For example, examplebucket.
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        $option = array(
            OssClient::OSS_KEY_MARKER => null,
            OssClient::OSS_VERSION_ID_MARKER => null,
            OssClient::OSS_DELIMITER => "/",    // Set the separator to a forward slash (/).
        );
        $bool = true;
        while ($bool){
            $result = $ossClient->listObjectVersions($bucket,$option);
            // View the version information of the objects.
            foreach ($result->getObjectVersionList() as $key => $info){
                printf("key name: {$info->getKey()}\n");
                printf("versionid: {$info->getVersionId()}\n");
                printf("Is latest: {$info->getIsLatest()}\n\n");
            }
    
            // View the version information of the delete markers.
            foreach ($result->getDeleteMarkerList() as $key => $info){
                printf("del_maker key name: {$info->getKey()}\n");
                printf("del_maker versionid: {$info->getVersionId()}\n");
                printf("del_maker Is latest: {$info->getIsLatest()}\n\n");
            }
    
            if($result->getIsTruncated() === 'true'){
                $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
                $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
            }else{
                $bool = false;
            }
        }
    } catch(OssException $e) {
        printf($e->getMessage() . "\n");
        return;
    }                   
  • List object versions in a specified directory

    The following code provides an example of how to list the versions of objects in the test directory of 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 set. 
    $provider = new EnvironmentVariableCredentialsProvider();
    // Replace yourEndpoint with the endpoint of the region where the bucket is located. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = "yourEndpoint";
    // Specify the bucket name. For example, examplebucket.
    $bucket= "examplebucket";
    
    $config = array(
            "provider" => $provider,
            "endpoint" => $endpoint,
            "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
            "region"=> "cn-hangzhou"
        );
        $ossClient = new OssClient($config);
    
    try{
        $option = array(
            OssClient::OSS_KEY_MARKER => null,
            OssClient::OSS_VERSION_ID_MARKER => null,
            OssClient::OSS_DELIMITER => "/",    // Set the separator to a forward slash (/).
            OssClient::OSS_PREFIX => "test/",   // List the versions of objects in the test folder.
        );
        $bool = true;
        while ($bool){
            $result = $ossClient->listObjectVersions($bucket,$option);
            // View the version information of the objects.
            foreach ($result->getObjectVersionList() as $key => $info){
                printf("key name: {$info->getKey()}\n");
                printf("versionid: {$info->getVersionId()}\n");
                printf("Is latest: {$info->getIsLatest()}\n\n");
            }
    
            // View the version information of the delete markers.
            foreach ($result->getDeleteMarkerList() as $key => $info){
                printf("del_maker key name: {$info->getKey()}\n");
                printf("del_maker versionid: {$info->getVersionId()}\n");
                printf("del_maker Is latest: {$info->getIsLatest()}\n\n");
            }
    
            if($result->getIsTruncated() === 'true'){
                $option[OssClient::OSS_KEY_MARKER] = $result->getNextKeyMarker();
                $option[OssClient::OSS_VERSION_ID_MARKER] = $result->getNextVersionIdMarker();
            }else{
                $bool = false;
            }
        }
    } catch(OssException $e) {
        printf($e->getMessage() . "\n");
        return;
    }