Path: blob/master/code_examples/php_examples/S3examples/s3-listing-object-keys.php
4084 views
<?php1// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.2// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE )34require 'vendor/autoload.php';56use Aws\S3\S3Client;7use Aws\S3\Exception\S3Exception;89$bucket = '*** Your Bucket Name ***';1011// Instantiate the client.12$s3 = new S3Client([13'version' => 'latest',14'region' => 'us-east-1'15]);1617// Use the high-level iterators (returns ALL of your objects).18try {19$objects = $s3->getPaginator('ListObjects', [20'Bucket' => $bucket21]);2223echo "Keys retrieved!" . PHP_EOL;24foreach ($objects as $object) {25echo $object['Key'] . PHP_EOL;26}27} catch (S3Exception $e) {28echo $e->getMessage() . PHP_EOL;29}3031// Use the plain API (returns ONLY up to 1000 of your objects).32try {33$result = $s3->listObjects([34'Bucket' => $bucket35]);3637echo "Keys retrieved!" . PHP_EOL;38foreach ($result['Contents'] as $object) {39echo $object['Key'] . PHP_EOL;40}41} catch (S3Exception $e) {42echo $e->getMessage() . PHP_EOL;43}444546