Upload a File to AWS S3 Bucket with PHP SDK

This is a sample PHP script on how to upload a file to AWS S3 using PHP SDK

This is a sample script on how to upload a file via the PHP AWS SDK

require('./aws/aws-autoloader.php');

use Aws\S3\S3Client;

$bucket = 'mybucketname';

$s3 = S3Client::factory(array(
    'region'  => 'us-west-2',
    'version' => 'latest',
    'credentials' => [
        'key'    => 'AWS_KEY',
        'secret' => 'AWS_SECRET'
    ]
));

$key = $_FILES['link']['name'];
$sourceFile = $_FILES['link']['tmp_name'];

$result = $s3->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => fopen($sourceFile, 'r+'),
    'ACL'          => 'public-read',
));

print_r($result);
echo $result['ObjectURL']; //return link of file


This is useful when uploading a file that comes from a form. 

Enjoy!