liip/LiipImagineBundle — pull request #60 by neilferreira (2012-03-19 15:03:25+00:00)
Title: Issue #56 - Added an Amazon S3 resolver

Reference #56? (I've never done pull requests before so unsure how referencing works)

I've created an Amazon S3 resolver for this bundle.

Usage:
Add the following to the autoloader, as instructed by https://github.com/KnpLabs/KnpGaufretteBundle

define("AWS_CERTIFICATE_AUTHORITY", true);

// AWS SDK needs a special autoloader
require_once __DIR__.'/../vendor/aws-sdk/sdk.class.php';

Setup your filter inside config.yml, with the amazon resolver:

liip_imagine:
filter_sets:
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: outbound }
amazon:
cache: amazons3
quality: 75
filters:
relative_resize: { widen: 400, mode: outbound }

Add 3 new keys to your config.yml:

liip_imagine.aws_key: "[key]"
liip_imagine.aws_secret_key: "[secret]"
liip_imagine.s3.bucket_name: "[bucketname]"

You will need to update your composer.yml to include KnpLabs's Gaurfrette bundle

"knplabs/gaufrette": "dev-master"

I'm unsure how we're meant to be including the aws-sdk using composer as I only just switched over to composer a few days ago, can someone tell me if you need to set it up to use a pear repo or what? You'll need the aws-sdk to test either way.

Once you've done your schema update, you can start using your filter with the amazon s3 resolver.

The resolver will add any images to its upload queue that are requested, in the period between the image hitting the queue and subsequent requests coming in for that image, it will be served from disk in the same way as the WebPathResolver works.

Once you've got images in the queue table, you can run the console command "liip:s3upload" which will upload the images to S3, and set their live URL on the record. You'll notice in the cron script that it unlinks the cached file that WebPathResolver creates so the apache -f rewritecond will no longer pick it up.

Once the resolver now sees that the requested image is on S3, it will send out a 302 redirect to the image's S3 location instead, if the image doesn't already exist in the user's browser cache.

Foreseeable issues that need suggestions:
- If the bucket is renamed, the files will still link to the old url and most likely 404 on amazon's end
- I believe gaufrette actually has another class to automatically set the ACL of the images when you ->write them, I just didn't get around to trying it out. For now I'm directly accessing the amazon sdk and updating the acl.
- Someone recommended using cloudfront custom origins instead of s3, I'm not too sure whats involved in doing that instead as I've always been using s3.