Direct Upload to S3 (with PHP & Composer)
This is a continuation in the ‘Direct Upload‘ series:
- First we began with a look at how you can directly upload a file to s3, talking it through in detail (13/10/2013).
- We later made another post, explaining how to handle multiple files and updating the code to use AWS’s signature V4 (7/3/2015).
Now we’re back with another improvement!
Instead of the copy and paste from a blog post solution we were advocating in past blog posts, we’ve now built a Composer package instead. With all the improvements we’ve made to the script over time it seemed a shame that it wasn’t easy for users to update and maintain (because we weren’t using a package manager).
All the information on the package, like how to install, use and contribute can be found on it’s Github page (but we’ll explain how to use it here as well).
How to Install
To install we just need to include the package in our
composer.json file. Here’s a command which will add it for you:
composer require eddturtle/direct-upload
We need to make sure composer’s autoloader is included for this to work as well (which if you’re already using composer it will be).
How to Use
Once installed, we can just call the package and create our uploader, like so:
$uploader = new \EddTurtle\DirectUpload\Signature( "YOUR_S3_KEY", "YOUR_S3_SECRET", "YOUR_S3_BUCKET", "eu-west-1" );
HTML (+ PHP)
<form action="<?php echo $uploader->getFormUrl(); ?>" method="POST" enctype="multipart/form-data"> <?php echo $uploader->getFormInputsAsHtml(); ?> <input type="file" name="file" multiple> </form>
This is an example gif, showing multiple files getting uploaded using our example repository.