Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/php_examples/S3examples/s3-listing-object-keys.php
4084 views
1
<?php
2
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE )
4
5
require 'vendor/autoload.php';
6
7
use Aws\S3\S3Client;
8
use Aws\S3\Exception\S3Exception;
9
10
$bucket = '*** Your Bucket Name ***';
11
12
// Instantiate the client.
13
$s3 = new S3Client([
14
'version' => 'latest',
15
'region' => 'us-east-1'
16
]);
17
18
// Use the high-level iterators (returns ALL of your objects).
19
try {
20
$objects = $s3->getPaginator('ListObjects', [
21
'Bucket' => $bucket
22
]);
23
24
echo "Keys retrieved!" . PHP_EOL;
25
foreach ($objects as $object) {
26
echo $object['Key'] . PHP_EOL;
27
}
28
} catch (S3Exception $e) {
29
echo $e->getMessage() . PHP_EOL;
30
}
31
32
// Use the plain API (returns ONLY up to 1000 of your objects).
33
try {
34
$result = $s3->listObjects([
35
'Bucket' => $bucket
36
]);
37
38
echo "Keys retrieved!" . PHP_EOL;
39
foreach ($result['Contents'] as $object) {
40
echo $object['Key'] . PHP_EOL;
41
}
42
} catch (S3Exception $e) {
43
echo $e->getMessage() . PHP_EOL;
44
}
45
46