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-request-aws.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\Sts\StsClient;
8
use Aws\S3\S3Client;
9
use Aws\S3\Exception\S3Exception;
10
11
$bucket = '*** Your Bucket Name ***';
12
13
$s3 = new S3Client([
14
'region' => 'us-east-1',
15
'version' => 'latest',
16
]);
17
18
// Retrieve the list of buckets.
19
$result = $s3->listBuckets();
20
21
try {
22
// Retrieve a paginator for listing objects.
23
$objects = $s3->getPaginator('ListObjects', [
24
'Bucket' => $bucket
25
]);
26
27
echo "Keys retrieved!" . PHP_EOL;
28
29
// Print the list of objects to the page.
30
foreach ($objects as $object) {
31
echo $object['Key'] . PHP_EOL;
32
}
33
} catch (S3Exception $e) {
34
echo $e->getMessage() . PHP_EOL;
35
}
36
37