php-awsHow can I use the AWS S3 S3Client library with PHP?
Using the AWS S3 S3Client library with PHP is relatively straightforward. Here is an example of code that can be used to list the objects in a bucket:
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
$objects = $s3->listObjects([
'Bucket' => 'my-bucket'
]);
foreach ($objects['Contents'] as $object) {
echo $object['Key'] . "\n";
}
?>
Code explanation
- Require the autoloader:
require 'vendor/autoload.php';
- Use the S3Client class:
use Aws\S3\S3Client;
- Create a new S3Client instance:
$s3 = new S3Client([
- Set the version and region:
'version' => 'latest', 'region' => 'us-east-1'
- List the objects in the bucket:
$objects = $s3->listObjects([ 'Bucket' => 'my-bucket' ]);
- Iterate over the results and output the object keys:
foreach ($objects['Contents'] as $object) {
echo $object['Key'] . "\n";
}
Helpful links
More of Php Aws
- How do I use PHP to create a ZIP file on AWS?
- How can I use AWS and Zksync together with PHP?
- How can I use Yum to install PHP on an Amazon EC2 instance?
- How can I use an AWS SQS Worker with PHP?
- How can I use the AWS API Gateway with PHP?
- How can I use AWS WAF to secure my PHP application?
- How can I use AWS PHP SDK without credentials?
- How can I use PHP to connect to an Amazon Aurora database?
- How do I generate an AWS Signature Version 4 with PHP?
- How do I determine the version of PHP I am running on AWS?
See more codes...