php-awsHow can I use PHP to upload files to Amazon S3?
Using PHP to upload files to Amazon S3 is relatively simple. The following example code demonstrates how to do this:
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
// Create a S3 client
$s3Client = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1'
]);
// Upload a file
$result = $s3Client->putObject([
'Bucket' => 'my-bucket',
'Key' => 'my-file.jpg',
'Body' => fopen('/path/to/file.jpg', 'r'),
'ACL' => 'public-read'
]);
The code above will upload a file located at /path/to/file.jpg
to the S3 bucket my-bucket
with the key my-file.jpg
and set the access control list (ACL) to public-read
.
The code consists of the following parts:
require 'vendor/autoload.php';
- This will include the SDK using the Composer autoloader.$s3Client = new Aws\S3\S3Client([...])
- This will create a S3 client with the specified parameters.$result = $s3Client->putObject([...])
- This will upload the file to the specified S3 bucket with the specified key and ACL.
For more information, please refer to the AWS SDK for PHP documentation.
More of Php Aws
- How can I use PHP to connect to an Amazon Aurora database?
- 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 AWS WAF to secure my PHP application?
- How can I use AWS PHP SDK without credentials?
- How do I generate an AWS Signature Version 4 with PHP?
- How do I determine the version of PHP I am running on AWS?
- How can I use the AWS S3 S3Client library with PHP?
See more codes...