如何使用AWS PHP sdk在JSON中检索Amazon S3存储桶的内容?

I want to get the list of all the filenames that are inside my Amazon S3 bucket from my PHP server. Is that possible with the Amazon PHP sdk? If not, is there another way? Thanks!

Using the official AWS SDK for PHP v2, you can do something like the following:

<?php

require 'vendor/autoload.php';

use Aws\Common\Aws;

// Instantiate an S3 client
$s3 = Aws::factory('/path/to/config.php')->get('s3');

$objects = $s3->getIterator('ListObjects', array(
    'Bucket' => $bucket
));

foreach ($objects as $object) {
    echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL;
}
  1. It worked fine for me except that MaxKeys was ignored.
  2. Is there a difference between using the above method and the one below. Although I couldn't get to array elements within $result.

    use Aws\Common\Aws;
    use Aws\S3\S3Client;
    
    // Prepare S3 Client to speak with AWS
    $s3client;
    $s3client = S3Client::factory(array(
        'key' => '',
        'secret' => '',
        'region' => Region::US_EAST_1
    ));
    
    // List objects from a bucket
    $result = $s3client->listObjects(array(
        'Bucket' => $aws_bucket,
        'MaxKeys' => 25,
        'Marker' => 'docs/'
    ));
    
    $objects = $result->getIterator();
    
    foreach ($objects as $wo)
    {
        // Prints junk
        echo $wo['Key'] . ' - ' . $wo['Size'] . PHP_EOL;
    }