Google trends Public Holidays Coupon Code Code Compiler

Upload image on Amazon S3 Bucket


Dec 25, 2018

Simple way to upload images on Amazon S3, If your website has too many image and looking for Amazon S3 to store images there to reduces file load time and bandwidth usage, then you’re here at right place. Amazon Simple Storage Service (Amazon S3) is a popular web service that provides highly scalable, durable and secure storage. It’s very easy to use and makes use of bucket to store and retrieve data from anywhere and anytime on the web. So here in this tutorial you will learn how to upload files to Amazon S3 using PHP CodeIgniter. The tutorial explained in very easy steps to Upload file on Amazon S3. So let’s start the coding. We will have following code for the upload files to Amazon S3 using PHP.

PHP Code:  upload.php

Define Amazon Keys:


define('AMAZON_BUCKET_NAME','Your amazon bucket name');
define('AMAZON_ACCESS_KEY','Your amazon access key');
define('AMAZON_SECRET_KEY','Your amazon secret key');

Include Amazon S3 Libraby:


include 'S3.php';


$image_name = $_FILES['img']['name'];
$uploadFile = $_FILES['img']['tmp_name'];
$bucketName = AMAZON_BUCKET_NAME;
$ext = pathinfo($image_name, PATHINFO_EXTENSION);
$s3_upload_name = 'mypic' . time() . '.' . $ext;
$s3 = new S3(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY);
$s3->putBucket($bucketName, S3::ACL_PUBLIC_READ); $amaz;
if ($s3->putObjectFile($uploadFile, $bucketName, $s3_upload_name, S3::ACL_PUBLIC_READ)) {
    // Get image URL like     $amaz . $bucketName . '/' . $s3_upload_name;   
}
echo $amazon_url;

HTML Code:  form.php


<html>
 <head>
  <meta http-equiv="Content-Type" c />
 </head>
 <body>
  <form action="upload.php" method="post" enctype="multipart/form-data">
   <div>
    <label>Upload image:</label>
    <input type="file"  name="img" />
    <button type="submit" id="submit" name="submit">Upload</button>
   </div>
  </form>
 <body>
</html>

Copyright 2024. All rights are reserved