php-awsHow can I use the PHP AWS S3 S3Client to upload files?
The AWS S3Client in PHP can be used to upload files to an Amazon S3 bucket. The following example code can be used to upload a file:
<?php
$s3Client = new Aws\S3\S3Client([
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
]
]);
$result = $s3Client->putObject([
'Bucket' => 'my-bucket',
'Key' => 'my-file.txt',
'Body' => 'this is the body!'
]);
echo $result['ObjectURL'];
?>
The code above will upload a file named my-file.txt
to the bucket my-bucket
with the body this is the body!
. The $s3Client
variable is an instance of the S3Client class. The putObject
method is used to upload the file, and the ObjectURL
of the uploaded file is returned in the $result
variable.
Code explanation
$s3Client
: An instance of the S3Client class.putObject
: The method used to upload the file.Bucket
: The name of the bucket to upload the file to.Key
: The name of the file to be uploaded.Body
: The body of the file to be uploaded.$result
: The variable that holds the result of theputObject
method.ObjectURL
: The URL of the uploaded file.
Helpful links
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...