Home |  MySQL Buzz |  FAQ |  Feeds |  Submit your blog feed |  Feedback |  Archive |  Aggregate feed RSS 2.0 English Deutsch Español Français Italiano 日本語 Русский Português 中文
How to use amazon s3 using php
+0 Vote Up -0 Vote Down

Amazon Simple Storage Service is designed to make web-scale computing easier for developers. If you have a medium to high traffic web application then S3 will help you to load your content faster. Here contents may be files, photos, videos or even text. Normally S3 is used for files, photos and videos. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites.

3 months ago I developed a project for one of my client using Zend Framework. 1 week ago he told me to change the photo uploading system from hosting disk to amazon s3. Yesterday I implemented s3 for him and found its really very easy using Zend framework.

Here I’m describing how did I do so:

If you want to access S3 you’ve to signup in amazon s3 and need to get access key and secret key.

Here is a simple example of the basic usage using php and zend framework:

$secret   = "XXXXXXXXXXXXXXX";
$access  = "YYYYYYYYYYYYYYY";
$bucket  = "photo";

require_once 'Zend/Service/Amazon/S3.php';
$s3 = new Zend_Service_Amazon_S3($secret, $access);

$s3->createBucket("$bucket");
$s3->putObject("$bucket/myobject", "somedata");
echo $s3->getObject("$bucket/myobject");

At first you’ve to create a object $s3 of Zend_Service_Amazon_S3 class by providing your access and secret keys. Then you have to create a bucket. You can think bucket as folder where you’ll put your data.

Now I’m showing you another example little modified from my project. In my project I’ve a form from where user could upload photos from their computer.

<form name="fphoto" action="<?=BASE_URL?>/photos/photoupload" method="POST" enctype="multipart/form-data">
                    <input type="file" name="photo" size="80" /> <br />
                    <input type="submit" value="Submit" name="submit" />
                </form>

Where BASE_URL is the web application url and photos is the controller name and photoupload is the action name of photos controller. Now I used the following code to upload files in s3.

class PhotosController extends Zend_Controller_Action{
    function photouploadAction(){
        $access         =   "xxxxxxxxxxxxxxxxx";
        $secret          =   "yyyyyyyyyyyyyyy";
        $bucket         =   "user-photos";
        try{
            $s3         =   new Zend_Service_Amazon_S3($access, $secret);
            if (!$s3->isBucketAvailable($bucket))
                $s3->createBucket($bucket);

        }catch(Exception $o){
            print_r($o);
        }
        if ($_FILES['photo']['error'] == UPLOAD_ERR_OK && !empty($_FILES['photo']['tmp_name'])) {
            //file photo
            $sourceFile     =   $_FILES['photo']['tmp_name'];
            $type           =   $_FILES['photo']['type'];
            $name           =   $_FILES['photo']['name'];
            $size           =   $_FILES['photo']['size'];

            if ( preg_match("!(?:jpeg|png|gif|bmp)!i", $type) ){
                //upload photo to director. now amazon s3

                $lastId     =   1; //it is dynamically generated in my project using mysql
                $newFilename=   $lastId . strstr($name, '.');

                try{
                     $s3->putObject("$bucket/$newFilename", file_get_contents($sourceFile),
                        array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
                }
                catch(Exception $o){
                    print_r($o);
                }
                $filepath     =   "http://s3.amazonaws.com/$bucket/$newFilename";
                //$filepath is the final path. I store it in my database for future reference

            }
        }
    }
}

Look I used file_get_contents() to read the source file. You can also provide url like http://somesite.com/image.jpg to this method to read the contents of that data. And then I store it in my bucket using $s3->putObject() method. When I put object in s3 I give public read permission so you can now see the image or file by just visiting http://s3.amazonaws.com/$bucket/$newFilename url. Where $newFilename will replaced by actual name.

I mention the codes here with little modification, actually I omit the database functionality here. But in my project I dynamically generate $lastId using mysql and store them and load them dynamically from S3. My intention was to show you how easy to use S3 service using php and zend framework.


Votes:

You must be logged in with a MySQL account to vote on Planet MySQL entries. More information on PlanetMySQL voting.

Planet MySQL © 1995, 2013, Oracle Corporation and/or its affiliates   Legal Policies | Your Privacy Rights | Terms of Use

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.