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!
Asked
Active
Viewed 5,108 times
2
-
1Possible duplicate: http://stackoverflow.com/questions/3337912/quick-way-to-list-all-files-in-amazon-s3-bucket – Babblo Feb 02 '13 at 23:07
2 Answers
4
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;
}

Ryan Parman
- 6,855
- 1
- 29
- 43
1
- It worked fine for me except that
MaxKeys
was ignored. 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; }

Kirk Beard
- 9,569
- 12
- 43
- 47

mishka
- 677
- 6
- 20