Accessing Amazon S3 with PHP using Amazon AWS SDK

Discussion in 'PHP' started by asha, Oct 5, 2012.

  1. asha

    asha New Member

    Joined:
    Nov 9, 2006
    Messages:
    44
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Homemaker
    Location:
    Kolkata
    Home Page:
    http://whatanindianrecipe.com
    Amazon Simple Storage Service (S3) is a low-cost & high-availability storage service provided by Amazon Web Services. It's very efficient and cheap, it allows you to store as much as you want and there is no limit, you pay for what you use and you pay as you go, there is no commitment. You can check their service details and pricing at http://aws.amazon.com/s3/

    In this article we'll look at accessing the service using PHP library provided by Amazon.

    Installing AWS PHP SDK



    There are more than one way to install the SDK, you may use the one that suits you.
    Download and install from http://aws.amazon.com/sdkforphp. You can get it from GitHub
    Code:
      # git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
      
    Getting it from the SVN repo
    Code:
      # svn co http://svn.github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
      
    The easiest and best solution as far as I think is installing using PEAR manager.
    Code:
      # pear channel-discover pear.amazonwebservices.com
      # pear install aws/sdk
      
    Except for PEAR installation method you might want to add the path to the SDK to the include_path.

    Usage



    Let's now go about looking at the usage of the SDK for some common operations on S3, I will not be able to list all here, but you may look the complete list of method at http://docs.amazonwebservices.com/AWSSDKforPHP/latest/

    Listing Buckets

    Follow the simple example below to list all the buckets in your S3 account.

    PHP:
      // include the library
      
    require_once 'AWSSDKforPHP/sdk.class.php';
     
      
    // instantiate the s3 object
      
    $s3 = new AmazonS3(array(
              
    'key' => '<your-AWS-key>',
              
    'secret' => '<your-AWS-secret>'
      
    ));
      
      
    // fetch buckets
      
    $response $s3->list_buckets();
      
      
    // iterate and print
      
    foreach ($response->body->Buckets->children() as $child) {
              echo 
    $child->Name[0] . "\n";
      }
      
    Listing Object in a Bucket

    List all the keys in a bucket, with and without specifying a prefix.

    PHP:
      // get all objects (maximum 1000)
      
    $response $s3->get_object_list('my_bucket');
      
      foreach (
    $response as $k) {
              echo 
    "$k\n";
      }
      
      
    // get object matching a prefix
      
    $response $s3->get_object_list('my_bucket', array('prefix' => 'image_'));
      
      foreach (
    $response as $k) {
              echo 
    "$k\n";
      }
      
    Upload a file

    You can upload files of any size, you can set permissions of the file while uploading them.
    PHP:
      // upload file
      
    $response $s3->create_object('my_bucket''files.tgz', array(
              
    'acl' => AmazonS3::ACL_PRIVATE,
              
    'fileUpload' => '/home/pradeep/files.tgz',
              
    'contentType' => 'application/x-compressed'
      
    ));
      
    Getting an object

    You can download a file to the filesystem, or have it in the object (if it's a small file).

    PHP:
      $response $s3->get_object('my_bucket''large_files.tgz', array(
          
    'fileDownload' => 'large_files.tgz'
      
    ));
      
    Deleting an object(s)

    You can delete a single object or multiple objects from a S3 bucket.

    PHP:
      // single object deletion
      
    $response $s3->delete_object ('my_bucket''large_files.tgz');
      
      
    // multiple object deletion
      
    $response $s3->delete_objects('my_bucket',array('objects' => array(array('key'=>'large_files.tgz'),array('key' => 'data.iso'))));
      
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice