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

Object operations

The object is the basic storage unit in S3. Object stores unstructured data, which can be any size up to 4 gigabytes. There's no limit on how many objects can be stored on the system.

The object are contained in buckets. Object is identified by name, which can be any utf-8 string. It is common to use hierarchical names (such as Pictures/Myself/CodingInPHP.jpg) to organise object names. Object name is prefixed with bucket name when using object functions, so for object "mydata" in bucket "my-own-bucket" the name would be my-own-bucket/mydata.

Objects can be replaced (by rewriting new data with the same key) or deleted, but not modified, appended, etc. Object is always stored whole.

By default, all objects are private and can be accessed only by their owner. However, it is possible to specify object with public access, in which case it will be available through the URL: http://s3.amazonaws.com/[bucket-name]/[object-name].

  • putObject($object, $data, $meta) created an object with name $object (should contain the bucket name as prefix!) having $data as its content.

    Optional $meta parameter is the array of metadata, which currently supports the following parameters as keys:

    S3_CONTENT_TYPE_HEADER

    MIME content type of the data. If not specified, the type will be guessed according to the file extension of the object name.

    S3_ACL_HEADER

    The access to the item. Following access constants can be used:

    S3_ACL_PRIVATE

    Only the owner has access to the item.

    S3_ACL_PUBLIC_READ

    Anybody can read the object, but only owner can write. This is setting may be used to store publicly accessible content.

    S3_ACL_PUBLIC_WRITE

    Anybody can read or write the object. This policy is rarely useful.

    S3_ACL_AUTH_READ

    Only the owner has write access to the item, and other authenticated S3 users have read access. This is useful for sharing data between S3 accounts without exposing them to the public.

    By default, all the items are private.

    Example 772. Zend_Service_Amazon_S3 Public Object Example

    <?php
    require_once 'Zend/Service/Amazon/S3.php';

    $s3 = new Zend_Service_Amazon_S3($my_aws_key$my_aws_secret_key);

    $s3->putObject("my-own-bucket/Pictures/Me.png"file_get_contents("me.png"),
        array(
    Zend_Service_Amazon_S3::S3_ACL_HEADER =>
              
    Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
    // or:
    $s3->putFile("me.png""my-own-bucket/Pictures/Me.png",
        array(
    Zend_Service_Amazon_S3::S3_ACL_HEADER =>
              
    Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ));
    echo 
    "Go to http://s3.amazonaws.com/my-own-bucket/Pictures/Me.png to see me!\n";

  • getObject($object) retrieves object data from the storage by name.

  • removeObject($object) removes the object from the storage.

  • getInfo($object) retrieves the metadata information about the object. The function will return array with metadata information. Some of the useful keys are:

    type

    The MIME type of the item.

    size

    The size of the object data.

    mtime

    UNIX-type timestamp of the last modification for the object.

    etag

    The ETag of the data, which is the MD5 hash of the data, surrounded by quotes (").

    The function will return FALSE if the key does not correspond to any existing object.

  • getObjectsByBucket($bucket) returns the list of the object keys, contained in the bucket.

    Example 773. Zend_Service_Amazon_S3 Object Listing Example

    <?php
    require_once 'Zend/Service/Amazon/S3.php';

    $s3 = new Zend_Service_Amazon_S3($my_aws_key$my_aws_secret_key);

    $list $s3->getObjectsByBucket("my-own-bucket");
    foreach(
    $list as $name) {
      echo 
    "I have $name key:\n";
      
    $data $s3->getObject("my-own-bucket/$name");
      echo 
    "with data: $data\n";
    }

  • isObjectAvailable($object) checks if the object with given name exists.

  • putFile($path, $object, $meta) puts the content of the file in $path into the object named $object.

    The optional $meta argument is the same as for putObject. If the content type is omitted, it will be guessed basing on the source file name.

Zend Framework