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 can I use AWS and Zksync together with PHP?
- How can I use an AWS SQS Worker with PHP?
- How can I use AWS WAF to secure my PHP application?
- How do I generate an AWS Signature Version 4 with PHP?
- How can I use AWS Textract with PHP?
- How can I connect to Amazon Web Services (AWS) S3 using PHP?
- How can I use PHP and AWS Transcribe to transcribe audio files?
- How can I use the PHP AWS SDK to send messages via SNS?
- How can I use the PHP AWS S3 S3Client to upload files?
See more codes...