Amazon S3 file Upload using PHP API

Amazon S3 Server provides large disk space for the web data storage the complete advantage of the cloud computing is another example of S3 sever .The Amazon S3 file upload using the php api is now pretty simple and most secure.The most advantage of the system is the code running server (EC2) and File storage Server (S3) is separate that means for every request for file it give the risk to S3 server so the risk on your EC2 sever will reduce and performance will improved. Here i just explain How we can upload a file (any type) to Amazon S3 server using php api. The fist step is you have to signup with the Amazon server for getting the credentials. check here.

Amazon S3 file Upload using php api
Amazon S3 file Upload using php api

The next step is to get the [label style=”1″]S3.php[/label]  for manage s3 operation from here. when you get the S3 [label style=”1″]access key[/label] and [label style=”1″]secret key[/label] then you can start playing on the S3 server with this AWS SDK with php.

Before starting with Amazon s3 file upload using php api ,just an explanation about buckets. The word bucket is used for representing the s3 server folders(kind of). These are unique and have two access level Private and Public. Based on this access level the file access provides. The bucket you can access like [code style=”1″]http://mybucket.s3.amazonaws.com/your_filename[/code]

For Basic Operations check the following codes.

File Upload From Your Sever(EC2).

[code style=”1″]

<?php

if (!class_exists(‘S3’))require_once(‘S3.php’);
//AWS access info
if (!defined(‘awsAccessKey’)) define(‘awsAccessKey’, ‘YourAccess S3 Key’);
if (!defined(‘awsSecretKey’)) define(‘awsSecretKey’, ‘Yor Secret Key’);
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
$s3->putBucket(“bucket name”, S3::ACL_PRIVATE); //sec param specify the access level

//move the file to s3 server
if ($s3->putObjectFile(“your file name in the server with path”, “which bucket ur using (bucket name)”, “fine name in s3 server”, S3::ACL_PRIVATE)) {

//s3 upload success

}

?>

[/code]

Get an object on s3

[code style=”1″]S3::getObject($bucketName, $uploadName)[/code]

For copy an object using

[code style=”1″]S3::copyObject($srcBucket, $srcName, $bucketName, $saveName, $metaHeaders = array(), $requestHeaders = array())[/code]

For Delete

[code style=”1″]S3::deleteObject($bucketName, $uploadName)[/code]

And Basic Operation on buckets are like

[code style=”1″]

S3::listBuckets() // Simple bucket list

S3::listBuckets(true) // Detailed bucket list

[/code]

For more details about the S3 operation you can read this.

Good Luck 🙂

6 Comments

  1. It`s nice to know how easy it is to upload files with php and that it give people how spend their time to write tutorials like this!

    But i have a problem, is it possible to get a file from an url? I need to upload a lot of images to the s3 storage and it would be nice to write a script, which get the files directly from an url without the roundabout way on my server…. ??

Leave a Reply

Your email address will not be published. Required fields are marked *