Amazon S3 File Upload Using PHP

Amazon S3 File Upload Using PHP

Did you ever face the low storage and hacking issues with your project? If yes then you are good here with me. Today, we will overcome this issue with Amazon S3. Let me introduce “Amazon S3” first further we will implement 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

Prerequisites for Amazon S3 File Upload

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

  • Amazon Account (Create Your Account)
  • PHP Knowledge

Step 1:

Download the files in your computer. Extracting the zip, you will find the files as below and You can download most recent version of Amazon PHP SDK by running following composer command.

  1. config_s3.php //Configuration file
  2. image_validation.php //Validation scripts
  3. index.php //Main index file

Step 2:

I assume that you have created your amazon account. Now , get your access keys.

Step 3:

Open your index.php file. This file contains HTML and PHP codes for submitting form.

Step 4:

Open your config_s3.php. Here you will provide your access keys.

Step 5:

Now create a bucket.  Select properties and then check on everyone in permissions option.

Step 6:

The file image_validation.php will help you to get file extensions.

So far, we have complete the download and installation process for AWS SDK for PHP. Now, we will discuss the process about creating the client.

Creating a client:

First, we will create a client object. We will use one of the following techniques.
  1. Factory method
  2. Listing your buckets
  3. Listing objects in your buckets
  4. Uploading a file
  5. Uploading objects
  6. Listing objects in your buckets
  7. Downloading objects
  8. Saving objects to a file
  9. Cleaning up

Factory Method

This is easiest way among all the methods which help us to run the quickly and efficiently. So, we will use the Aws\S3\S3Client::factory()method and by using profile option, we will provide our credentials.

Here, we can give our credential profile, specify the access keys directly i.e (via key and secret).

Listing your buckets

A method listBuckets is used to list our all the buckets owned by our account.

An object Guzzle\Service\Resource\Model is returned if we use the AWS SDK for PHP return . This object will have all of the data returned from this service. This object willl also contain a get() method. This method is used to retrieve the values from the model by name. Moreover, the rested vlaues will be retrieve using getPath() method.

Listing objects in your buckets

This method is a lost of an easier way to list the objects in buckets. Because new SDK with iterators helps us. For this, we will use ListObjectsIterator.

This iterator (ListObjects) also works with other parameters.

Uploading a file

By passing the SourceFile option,we can upload the contents of a file.


Download Source Code


Download Amazon Full PHP SDK

Read Other Interesting Articles

Amazon S3 Multipart Uploads File using PHP

Connect to Amazon EC2 from windows using PuTTY