Skip to main content

Amazon S3 File Upload Using PHP

What is Amazon S3?
Amazon Simple Storage Service (Amazon S3), S3 stands for “Simple Storage Service”, is an amazon service which provides us a highly scalable, durable and secure storage. It is easy and simple to use with a simple web service interface. We can store and retrieve our data from anywhere and anytime on the web. It makes buckets for us so we can store our data in our buckets.

Amazon S3 is an ideal option that reduces file load time and bandwidth usage. We know that uploading function is the sensitive part in any web project, a little bit mistake will allow hackers to upload the malicious files. Amazon S3 will provide you the safe side. Learn more at

http://aws.amazon.com/s3/.


Benefits of Cloud Object Storage
Storing data on an AWS Cloud object storage service delivers advantages in three key areas:

1. Durability, Availability, & Scalability.

2. Security & Compliance.

3. Flexible Management.

4. Query-in-Place.

5. Broadest Ecosystem.


best video from amazon when you integration of amazon S3 :




Get started with Amazon S3

1-Sign up for an AWS account link : https://aws.amazon.com/free/?pg=ln&p=s3

2-Learn with 10-minute Tutorials link : https://aws.amazon.com/getting-started/tutorials/?pg=ln&p=s3

3-Start building with AWS link : https://aws.amazon.com/getting-started/projects/?pg=ln&p=s3


You can download most recent version of Amazon PHP SDK by running following composer command

Amazon Account (Create Your Account)
PHP Knowledge


Following steps follow :

1-create project as per you like name 

2-open command prompt run following code :


composer require aws/aws-sdk-php


3-after donwload or run above code then open your project file or path

4-make following files and put the key and secret code in this files 
config.php 


<?php
// Bucket Name
$bucket="BucketName";

//AWS access info
if (!defined('API_KEY')) define('API_KEY', 'ACCESS KEY');
if (!defined('API_SECRET')) define('API_SECRET', 'ACCESS SECRET KEY');
if (!defined('BUKETNAME')) define('BUKETNAME', 'BUCKET NAME');

    // Set Amazon s3 credentials
      $client = S3Client::factory(
      array(
      'key'    => API_KEY,
      'secret' => API_SECRET
       )
      );
?>


5-create home.php files and put below code :

<?php
require 'vendor/autoload.php';
require_once 'config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
    //
$s3 = S3Client::factory(
        array(
            'credentials' => array(
                'key' => API_KEY,
                'secret' => API_SECRET
            ),
            'version' => 'latest',
            'region'  => 'ap-south-1'
        )
    );
?>


above code amazon s3 all property access and mention region name as per your location

below function show listing of directory or bucket name :
<?php
//Start::bucket list::-
 $result = $s3->listBuckets();
foreach ($result['Buckets'] as $bucket) {
   // Each Bucket value will contain a Name and CreationDate
     echo "{$bucket['Name']} - {$bucket['CreationDate']}\n";
 }
//End::bucket list::-
?>


in you project paste image file such as example : test.jpg , when you upload files on amazon s3 server then use following code :
Single image upload on amazon web server :
<?php
//Start::single image upload on server::
$filepath = __DIR__ . '/test.jpg';
$key=basename($filepath);
//
$tmp="test.jpg";
try {
   $finalreturn= $s3->putObject([
        'Bucket' => 'blissbasket',
        'Key'    => $key,
        'SourceFile' => $tmp,
    ]);
   //image name
$imgname=$finalreturn['ObjectURL'];

} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
}
// echo '<pre>';
// print_r($imgname);
// die;
?>



when you upload on subdirectory then first create folder and then move on this particular folder :
<?php
//Start::image upload folder on server::
 $filepath = __DIR__ . '/1_1/';
 $uploadsfiles=$s3->uploadDirectory($filepath, BUKETNAME);
//End:::image upload folder on server::
//Start::create folder on server::
 $uploadsfiles=$s3->putObject(array( 
                    'Bucket' => BUKETNAME,
                    'Key'    => "1_2/",
                    'Body'   => "",
                    'ACL'    => 'public-read'
                   ));
 $folderpath=$uploadsfiles['ObjectURL'];
// echo '<pre>';
// print_r($folderpath);
// die;
//End::create folder on server::
?>


then upload on particular folder image using following code:

<?php
//Start::single image upload on server::
$filepath = __DIR__ . '/1_1/test.jpg';
$key=basename($filepath);
//
$tmp="test.jpg";
try {
   $finalreturn= $s3->putObject([
        'Bucket' => 'blissbasket',
        'Key'    => '1_1/'.$key,
        'SourceFile' => $tmp,
    ]);
   //image name
$imgname=$finalreturn['ObjectURL'];

} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
}
// echo '<pre>';
// print_r($imgname);
// die;
//End::single image upload on server::
?>


following full code of image upload 

<?php

require 'vendor/autoload.php';
require_once 'config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
 //
$s3 = S3Client::factory(
        array(
            'credentials' => array(
                'key' => API_KEY,
                'secret' => API_SECRET
            ),
            'version' => 'latest',
            'region'  => 'ap-south-1'
        )
    );
//Start::single image upload on server::
$filepath = __DIR__ . '/1_1/ganesh.jpg';
$key=basename($filepath);
//
$tmp="ganesh.jpg";
try {
   $finalreturn= $s3->putObject([
        'Bucket' => 'blissbasket',
        'Key'    => '1_1/'.$key,
        'SourceFile' => $tmp,
    ]);
   //image name
$imgname=$finalreturn['ObjectURL'];

} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
}
?>



if you want any help let me know or mail me : disuzajen@gmail.com

Comments

Popular posts from this blog

using PDO database connection add,update,delete,edit operation

PDO advantage : 1-Object Oriented 2-Bind parameters in statements (security) 3-Allows for prepared statements and rollback functionality (consistency) 4-Throws catcheable exceptions for better error handling (quality) 5-Exception mode; no need to check error state after each API call. It's best to tell PDO how you'd like the data to be fetched. You have the following options: 1-PDO::FETCH_ASSOC: returns an array indexed by column name. 2-PDO::FETCH_BOTH: (default):returns an array indexed by both column name and number. 3-PDO::FETCH_BOUND:Assigns the values of your columns to the variables set with the ->bindColumn() method. 4-PDO::FETCH_CLASS: Assigns the values of your columns to properties of the named class. It will create the properties if matching properties do not exist. 5-PDO::FETCH_INTO:Updates an existing instance of the named class. 6-PDO::FETCH_LAZY: Combines. 7-PDO::FETCH_BOTH/PDO:FETCH_OBJ, creating the object variable names as t...

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links

Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links user want to share any information then use following code  and read step by step Profile Share Fixing the Thumbnail Image, Title and Description for Shared Links if you want share profile on following social link : 1-Facebook 2-twitter.com 3-LinkedIn 4-google +, Code link : https://drive.google.com/open?id=1IzTZZh_0euDqFSHL_vPRiQePlNTw3h-q Demo link : http://freeteachnology.hol.es/socialshare/ To modify a page's thumbnail image, description, and additional metadata for these services, you can provide meta tags in the HTML code of the page.Implementing Open Graph Meta Tags You can implement meta tags in a number of ways. In content management systems might  be allow you to modify a page's meta tags , then use following code in meta section of your project code, <link href="bootstrap.min.css" rel="stylesheet"> <link href="bootstrap-tour.m...

GUID for globally unique identifier

How to create GUID in php 1-guid stands for globally unique identifier generally used to create random unique strings in php, create access token in php 2-Mostly use of GUID for generating access token, generate unique id, generating unique string in php. Using this article how to create guide in php you can create a random string for any use to keep unique 3-GUID consists of alphanumeric characters only and is grouped in five groups separated by hyphens as seen in this example: 3F2504E0-4F89-11D3-9A0C-0305E82C3301 Eg:- <?php /** * Generate Globally Unique Identifier (GUID) * E.g. 2EF40F5A-ADE8-5AE3-2491-85CA5CBD6EA7 * * @param boolean $include_braces Set to true if the final guid needs * to be wrapped in curly braces * @return string */ function generateGuid($include_braces = false) { if (function_exists('com_create_guid')) { if ($include_braces === true) { return com_create_guid(); } else { return substr(com_cr...