Blog

Zend Framework: Amazon S3 Component

With the 1.8 release, the Zend Framework has pushed out a number of really awesome components. One that I got to work with recently, was Zend_Service_Amazon_S3. At the time that I started using the S3 component, it was still in the incubator. Since it has been released, the official documentation can be found here. Although the documentation is pretty thorough, there are a couple points-of-interest that I think are worth mentioning.

1. Global namespace. Amazon S3 is set up such that all accounts share the same namespace. So this means that if you create a bucket in your S3 account, that bucket name can’t already exist. I suggest picking a unique bucket name and then using that as a root “directory” for all your S3 files.

2. Sub-Buckets. In S3, there’s no way to create a bucket within a bucket. But there is an easy way to mimic that kind of functionality. All you have to do is name your objects as though they are part of a bucket path. E.g. if your bucket name is ‘offshoot’, and you’d like to create a sub-bucket called ‘uploads’ and put an object called ‘file.jpg’ in your ‘uploads’ bucket, you would just name your object ‘uploads/file.jpg’. The code would look something like this:

$s3 = new Zend_Service_Amazon_S3($myAwsKey, $myAwsSecret);
$s3->putObject(‘offshoot/uploads/file.jpg’, ‘imagedata…’);

This will give the appearances of having a sub-bucket called ‘uploads’ within your root bucket. This should help you keep your objects nicely organized, as you would expect from any file system.

3. Pay attention to the ACL Headers. Amazon S3 has a pretty solid ACL for controlling who can do what with the files that you upload.

(Taken from the Zend Framework documentation):
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.

Make sure to use these to your advantage!

————————-

Overall, I’m really impressed with the quality of the components that ZF has been pushing out in the last little while. I’m looking forward to digging into the the Amazon EC2 component next!

*
To prove that you're not a bot, enter this code
Anti-Spam Image