As requirement, install the AWS SDK for PHP library to connect AWS S3.
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$config =
[
'version' => 'latest',
'region' => '',
'endpoint' => '[cluster-url]',
'credentials' =>
[
'key' => '[access-key]',
'secret' => '[secret-key]',
],
];
$client = new \Aws\S3\S3Client($config);
$result = $client->listObjects([
'Bucket' => 'bucket-name',
'Prefix' => 'sub/directory/'
]);
foreach ($result['Contents'] as $key => $object) {
// Is the value of prefix.
if (0 === $key) {
continue;
}
echo $object['Key'] . "<br>";
}