Files/Images upload into Amazon S3 using PHP

Amazon Simple Storage Service (Amazon S3) is most popular cloud platform to host web apps and datas .This PHP tutorial help to upload file to Amazon s3 server using web service and PHP.

Amazon Web Services (AWS) provides a low cost, reliable and secure foundation for you to use as you build and deliver Software as a Service (SaaS) solutions to the customers.There are a lot of techies uses amazon cloud to host images and files on amazon s3 server, these help to easy and fast access of resource and reduces file load time with bandwidth.

Amazon s3 providing simple s3 libs in php to access amazon resources, It’s very easy to use and integrate with any programming languages.I love amazon docs its so simple and understandable, Here in this tutorial you will learn how to upload files to Amazon S3 using PHP.

Lets start image upload tutorial for amazon S3 using PHP. We will create following files into this tutorial,
index.php – This file is main entry app file and have HTML layout to upload image.
config.php – This file has all configurations variable like bucket name, secret key etc.
s3.php – The Amazon S3 libs file.

Step 1: Create Amazon S3 Account
We will create Amazon S3 account and get bucket name and access keys to use for uploading files/images.

Step 2: Configure Amazon S3 With PHP
We have amazon S3 account details and now integrate with php. We will open config.php file and added bucket name, access key and secret key.We will add s3 libs as well to configure amazon S3 account.

// Bucket Name
$bucket="restapiexample";
require_once('S3.php');
//AWS access info
$awsAccessKey = 'Access Key';
$awsSecretKey = 'Security Key';
//instantiate the class
$s3 = new S3($awsAccessKey, $awsSecretKey);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
?>

Step 3: Create Image File Upload Form
Open index.php file and added image upload input form to upload image.

Amazon S3 File Upload using PHP


Upload Image


if($file_upload_message) { echo $file_upload_message; } ?>

Step 4: Handle Amazon S3 File Upload
We have added image upload form into index.php file, now we will add php logic to handle post form request and upload image to S3 using php. We will include s3.php libs here as well, its dependency libs to upload files to amazon S3 server.I am using S3 function putObjectFile() and display file upload message.

$file_upload_message = '';
if(isset($_POST["upload_files"])) {
$file_name = $_FILES['upload_file']['name'];
$file_size = $_FILES['upload_file']['size'];
$tmp_file = $_FILES['upload_file']['tmp_name'];
$valid_file_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
list($txt, $file_extension) = explode(".", $file_name);
if($file_name) {
if(in_array($file_extension,$valid_file_formats)) {
if($file_size < (1024*1024)) {
include('config.php');
$new_image_name = time().".".$file_extension;
if($s3->putObjectFile($tmp_file, $bucket , $new_image_name, S3::ACL_PUBLIC_READ)) {
$file_upload_message = "File Uploaded Successfully to amazon S3.

"; $uploaded_file_path='http://'.$bucket.'.s3.amazonaws.com/'.$new_image_name; $file_upload_message .= 'Upload File URL:'.$uploaded_file_path."
"; $file_upload_message .= ""; } else { $file_upload_message = "
File upload to amazon s3 failed!. Please try again."; } } else { $file_upload_message = "
Maximum allowed image upload size is 1 MB."; } } else { $file_upload_message = "
This file format is not allowed, please upload only image file."; } } else { $file_upload_message = "
Please select image file to upload."; } } ?>