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

Listing Objects

You can retrieve a list of objects in your bucket using the getObjectsByBucket() method. This method accepts as its first argument the name of the bucket and optionally accepts an array of parameters as its second argument.

The parameters allow you to paginate results, but for now we'll just look at the prefix parameter. Specifying this parameter allows you to retrieve all objects that begin with a certain name. If you look at objects having a filesystem structure (that is, organised into folders and files), you could retrieve every single file within a given directory.

The following listing demonstrates how to get files within a particular directory. We'll assume there's a "directory" in our bucket with the name myDir and that there are a number of files and sub-directories in there.

Listing 9 Getting a list of objects (get-objects-prefix.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';
 
    $ret = $s3->getObjectsByBucket($bucketName, array(
        'prefix' => 'myDir/'
    ));
 
    var_dump($ret);
?>
 
Output:
 
array
  0 => string 'myDir/file1.txt' (length=15)
  1 => string 'myDir/file2.txt' (length=15)
  2 => string 'myDir/subDir/file3.txt' (length=22)

This listing retrieves every single object with the given prefix. On the other hand, if you only want to retrieve objects that aren't in a sub-directory (so based on the output of the previous listing, not including the subDir directory), you can also specify the delimiter parameter.

This works by excluding objects in which the specified delimiter string appears. The following listing demonstrates listing only files in the specified directory.

Listing 10 Getting a list of objects in a single directory (get-objects-delimiter.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';
 
    $ret = $s3->getObjectsByBucket($bucketName, array(
        'prefix'    => 'myDir/',
        'delimiter' => '/'
    ));
 
    var_dump($ret);
?>
 
Output:
 
array
  0 => string 'myDir/file1.txt' (length=15)
  1 => string 'myDir/file2.txt' (length=15)

In This Article



Bonus listings: 2 available