php-awsHow can I use the PHP AWS SDK to put an object into an S3 bucket?
The AWS SDK for PHP provides a set of libraries to interact with Amazon Web Services (AWS) services, such as S3. To put an object into an S3 bucket, use the putObject
method from the Aws\S3\S3Client
class.
Example code
<?php
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
// Create an S3Client
$s3Client = new Aws\S3\S3Client([
'region' => 'us-west-2',
'version' => 'latest',
'credentials' => [
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
]
]);
// Upload an object
$result = $s3Client->putObject([
'Bucket' => 'my-bucket',
'Key' => 'my-object',
'Body' => 'this is the body!'
]);
echo $result['ObjectURL'];
Output example
https://my-bucket.s3.amazonaws.com/my-object
The code above does the following:
- Includes the SDK using the Composer autoloader.
- Creates an S3Client using the provided credentials.
- Uploads an object to a specified bucket using the putObject method.
- Prints the ObjectURL of the uploaded object.
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...