Upload File to Amazon Cloud Server using AWS

To upload any file to amazon EC2  S3 cloud server using PHP code you simply need to include AWS package(SDK) and include necesary files into your code.

Below is the function to upload file directly to S3 Server:

function uploadFileToS3($file, $file_name){
// Here $file is a $_FILE array
// and $file_name is the name of file you need to create
try {
$s3Client = S3Client::factory(array(
‘key’ => API_KEY, // defines your API KEY of EC2
‘secret’ => API_SECRET // defines your API SECRET of EC2
} catch (S3Exception $e) {
echo CONNECT_S3;

$filename = “uploads/files/” . $file_name;
$uploader = UploadBuilder::newInstance()
->setMinPartSize(5 * 1024 * 1024)
->setOption( ‘ContentType’, $file[‘image’][‘type’])
->setOption(‘ACL’, ‘public-read’)
return true;


Now I will simply call the above function by passing 2 arguments i.e. $_FILES array and the file name which you need to create on S3 server.


$file_name = ‘my-new-image.png’;

$response = uploadFileToS3($_FILES, $file_name);

NOTE: Do not  forgot to include AWS package(SDK) on the top of your code.

You can download aws.zip  from AWS Github

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s