列举对象

您可以使用 listObjects 接口列举对象,每次最多返回1000个对象,以下代码展示如何简单列举对象:

$bucket = '<your-bucket-name>';
try {
    $objects = $s3->listObjects([
        'Bucket' => $bucket
    ]);
    foreach ($objects['Contents']  as $object) {
        echo $object['Key'] . "\n";
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

如果 list 大于1000,则可以使用 getPaginator 接口列举所有对象。列举所有对象示例代码如下:

$bucket = '<your-bucket-name>';
try {
    $results = $s3->getPaginator('ListObjects', [
        'Bucket' => $bucket
    ]);
    foreach ($results as $result) {
        foreach ($result['Contents'] as $object) {
            echo $object['Key'] . "\n";
        }
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}