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.
The next step is to get the S3.php for manage s3 operation from here. when you get the S3 access key and secret key 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
http://mybucket.s3.amazonaws.com/your_filename
For Basic Operations check the following codes.
File Upload From Your Sever(EC2).
<?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
}
?>
Get an object on s3
S3::getObject($bucketName, $uploadName)
For copy an object using
S3::copyObject($srcBucket, $srcName, $bucketName, $saveName, $metaHeaders = array(), $requestHeaders = array())
For Delete
S3::deleteObject($bucketName, $uploadName)
And Basic Operation on buckets are like
S3::listBuckets() // Simple bucket list
S3::listBuckets(true) // Detailed bucket list
For more details about the S3 operation you can read this.
Good Luck 🙂
6 thoughts on “Amazon S3 file Upload using PHP API”
Nice Article. How it help to developer in terms of balance the day to day life.
you can find articles to upload files with s3 but what about uploading files to ec2 for first time.
EC2 is your code server so simple php script will be use to upload files to your EC2 server.
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…. ??
You can work like that,
directly upload your files to S3 server without using a bridge only thing in this case you have to take care about is security, you will get only client side validation options.
Amazon giving a brief detailed documentation for this kind of approach.
http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingHTTPPOST.html
http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTExamples.html
I never found a updated Script with Amazons latest sdk. i have made it by myself. it woks as a php commandline interpreter script. give it a try : https://github.com/arizawan/aiss3clientphp