PhpRiot
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

Zend Framework 101: Zend_Service_Amazon_S3

Creating, Modifying and Removing Objects

There are three ways to send an object to your S3 bucket: using putObject(), putFile() and putFileStream(). I'll demonstrate each of these shortly.

The first thing to know about objects though is that you can set certain attributes about each object, such as its availability to other uses. By default, all objects are private, meaning only you can read them via the API.

Since we're setting up a basic content delivery network, we're going to make all objects available for reading only. To achieve this, we set the S3_ACL_HEADER parameter to S3_ACL_PUBLIC_READ. The default setting is S3_ACL_PRIVATE. You can also manually set the content type of the uploaded data, but if you don't it will be automatically guessed (we'll let it guess in these examples).

To begin with, let's look at putObject(). This method takes three arguments: the object name, the object data, and the parameters. The bucket name is specified as the beginning of the object name. So if you wanted to create an object called my-test.txt (and using our sample bucket name of phpriot-test-bucket), the full object name is phpriot-test-bucket/my-test.txt.

The following listing demonstrates this. It calls putObject() with some arbitrary data and writes it to the object my-test.txt. If writing the data succeeds then true is returned from putObject(), otherwise false is returned.

Listing 6 Writing data to an object using putObject (put-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';
    $perms      = array(
        Zend_Service_Amazon_S3::S3_ACL_HEADER =>
            Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ
    );
 
    $ret = $s3->putObject(
        $bucketName . '/my-file.txt',
        'Some test data',
        $perms
    );
 
    echo "Success: " . ($ret ? 'Yes' : 'No');
?>

Similar to putObject(), we can send a file using the putFile() method. Instead of specifying data (such as from a PHP variable), we pass a filesystem path instead. Note however the arguments are swapped around from putObject(). That is, the local path is the first argument and the object name is the second argument.

If the local file cannot be found or read then an exception is thrown. The following listing demonstrates this functionality. It assumes we have a file called my-local-file.txt. We'll upload it to the bucket with the same name.

Listing 7 Writing a local file to an object using putFile (put-file.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';
    $filename   = 'my-local-file.txt';
    $perms      = array(
        Zend_Service_Amazon_S3::S3_ACL_HEADER =>
            Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ
    );
 
    $ret = $s3->putFile(
        $filename,
        $bucketName . '/' . $filename,
        $perms
    );
 
    echo "Success: " . ($ret ? 'Yes' : 'No');
?>

Finally, you can use the putFileStream() method to upload a local file. This is a better solution than using putFile() since the file isn't read into memory before being sent.

Once again you can pass the local path name, but instead of calling putFile() simply call putFileStream().

Modifying an Existing Object

To modify an object you can simply call one of these methods with the same object name and the old data will be replaced.

Removing an Object

You can remove an object from a bucket using the removeObject() method. This method accepts as its only argument the name of the object (including the name of the bucket as the start).

In This Article



Bonus listings: 2 available