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

Lists the versions of all objects in a bucket.

The following code provides an example on how to list the versions of all objects including delete markers within 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\OssClient;
use OSS\Core\OssException;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a Resource Access Management (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";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null
    );
    $bool = true;
    while ($bool){
        $result = $ossClient->listObjectVersions($bucket,$option);
        // Display the information about the versions of the listed 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");
        }

        // Display the version information of the listed 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 the versions of objects whose names contain a specified prefix

The following code provides an example on how to list the versions of objects whose names contain the "test" prefix in the bucket named 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;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null,
        // Specify that all versions of objects whose names contain the "test" prefix are listed. 
        OssClient::OSS_PREFIX => "test"
    );
    $bool = true;
    while ($bool){
        $result = $ossClient->listObjectVersions($bucket,$option);
        // Display the information about the versions of the listed 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");
        }

        // Display the version information of the listed 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 on 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\OssClient;
use OSS\Core\OssException;

// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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";
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);

try{
    $option = array(
        OssClient::OSS_KEY_MARKER => null,
        OssClient::OSS_VERSION_ID_MARKER => null,
        OssClient::OSS_MAX_KEYS => 200   // Specify that the maximum number of object versions that you can list is 200. 
    );
    $result = $ossClient->listObjectVersions($bucket,$option);
    // Display the information about the versions of the listed 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);
    }

    // Display the version information of the listed 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;
}

List objects in a directory

OSS does not use a hierarchical structure for objects, but instead uses a flat structure. All elements are stored as objects in buckets. A folder 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 (/) is displayed as a folder in the OSS console.

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

  • If you set prefix to a folder name in the request, objects and subfolders whose names contain the prefix are listed.
  • If you also set delimiter to a forward slash (/) in the request, the objects and subfolders whose names start with the specified prefix in the folder are listed. Each subfolder is listed as a single result element in commonPrefixes. The objects and folders in these subfolders are not listed.
  • List the versions of objects within the root directory of a bucket

    The following code provides an example on how to list the versions of objects within the root directory of the bucket named 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;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    
    try{
        $option = array(
            OssClient::OSS_KEY_MARKER => null,
            OssClient::OSS_VERSION_ID_MARKER => null,
            OssClient::OSS_DELIMITER => "/",    // Specify that the delimiter is forward slashes (/). 
        );
        $bool = true;
        while ($bool){
            $result = $ossClient->listObjectVersions($bucket,$option);
            // Display the information about the versions of the listed 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");
            }
    
            // Display the version information of the listed 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 the versions of objects within a specified directory of a bucket

    The following code provides an example on how to list the versions of objects within 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\OssClient;
    use OSS\Core\OssException;
    
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. 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";
    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    $endpoint = "yourEndpoint";
    // Specify the name of the bucket. Example: examplebucket. 
    $bucket= "examplebucket";
    
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
    
    try{
        $option = array(
            OssClient::OSS_KEY_MARKER => null,
            OssClient::OSS_VERSION_ID_MARKER => null,
            OssClient::OSS_DELIMITER => "/",    // Specify that the delimiter is forward slashes (/). 
            OssClient::OSS_PREFIX => "test/",   // Specify that the versions of objects within the test directory are listed. 
        );
        $bool = true;
        while ($bool){
            $result = $ossClient->listObjectVersions($bucket,$option);
            // Display the information about the versions of the listed 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");
            }
    
            // Display the version information of the listed 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;
    }