Accessing Amazon S3 with PHP using Amazon AWS SDK

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

  1. asha

    asha New Member

    Nov 9, 2006
    Likes Received:
    Trophy Points:
    Home Page:
    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

    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 You can get it from GitHub
      # git clone git:// AWSSDKforPHP
    Getting it from the SVN repo
      # svn co AWSSDKforPHP
    The easiest and best solution as far as I think is installing using PEAR manager.
      # pear channel-discover
      # pear install aws/sdk
    Except for PEAR installation method you might want to add the path to the SDK to the include_path.


    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

    Listing Buckets

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

      // 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) {
    $child->Name[0] . "\n";
    Listing Object in a Bucket

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

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

    You can upload files of any size, you can set permissions of the file while uploading them.
      // 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).

      $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.

      // 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