PhpRiot
Follow phpriot on Twitter
Sponsored Link
Download Article
Download this article or the entire “Zend Framework 101” series with all listings and files.

Includes 2 bonus listings!




More information
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Free iPad/iPhone App
Available on the App Store

  • PHP manual
  • Zend Framework manual
  • Smarty manual
  • PHP articles
  • PHP training

Related Books
PHP Solutions: Dynamic Web Design Made Easy

PHP Solutions: Dynamic Web Design Made Easy

This is the second edition of David Power's highly-respected PHP Solutions: Dynamic Web Design...

Cloud Computing Bible

Cloud Computing Bible

The complete reference guide to the hot technology of cloud computing Its potential for...

Zend Framework 101: Zend_Service_Amazon_S3

Reading Objects

The methods used to retrieve objects from a bucket are getObject() and getObjectStream(). You can use getObject() to return the object data into a PHP variable, while getObjectStream() is most useful for writing an object directly to your local filesystem.

For now, we'll just look getObject(). This method accepts as its only argument the name of the object, and returns the data found in the object. If the object can't be returned then false is returned instead.

The following listing demonstrates combining a call to getInfo() and getObject() so you can send the file back to the end-user. It retrieves the information about the file first so it can determine the mime type and file size.

Listing 11 Sending an object directly from an S3 bucket (get-object.php)
<?php
    require_once('Zend/Service/Amazon/S3.php');
 
    $awsKey       = '[your key]';
    $awsSecretKey = '[your secret key]';
 
    $s3 = new Zend_Service_Amazon_S3($awsKey, $awsSecretKey);
 
    $bucketName = 'phpriot-test-bucket';
    $objectName = $bucketName . '/my-file.txt';
 
    $info = $s3->getInfo($objectName);
 
    if (is_array($info)) {
        header('Content-type: ' . $info['type']);
        header('Content-length: ' . $info['size']);
 
        echo $s3->getObject($objectName);
    }
    else {
        header('HTTP/1.0 404 Not Found')
    }
?>

This example is really just for demonstration purposes only. If you were using Amazon S3 as a content delivery network it would be completely redundant (not to mention much slower) to read in the files and send to users as requested.

In This Article



Bonus listings: 2 available