Configuring Symfony 2 for Gaufrette and Amazon S3


We’ve got a really big project that’s being built in Symfony 2. It’s going to be hosted on Amazon Web Services (AWS), so I looked into integrating S3 into our build. The KnpLabsGaufretteBundle seems to be the right choice for the job due to its clean abstraction, but getting it to actually run was a bit trickier than it should have been.

The Gaufrette documentation is, well, a bit lacking. Here’s how I got it to work:

  1. Add the following to your composer.json:
    "knplabs/knp-gaufrette-bundle": "0.2.*@dev",
    "amazonwebservices/aws-sdk-for-php": "dev-master",

    and run composer update.

  2. Add the following to app/config/config.yml:
    knp_gaufrette:
        adapters:
            amazon:
                amazon_s3:
                    amazon_s3_id: amazon.s3
                    bucket_name: %amazon.bucket%
                    create: true
        filesystems:
            amazon:
                adapter: amazon
                stream_wrapper:
                    protocol: s3
                    filesystems:
                        - amazon
  3. Add the following to app/config/services.yml. If you don’t have one, create one and then go intoapp/config/config.yml and add an import line to the top.
    services:
        amazon.s3:
            class: AmazonS3
            arguments:
                options: { key: %amazon.key%, secret: %amazon.secret%, certificate_authority: false, default_cache_config: true }
  4. Then go into app/config/parameters.yml and add the following in, putting in the proper values from your AWS account:
    amazon.key: YOUR_KEY
    amazon.secret: YOUR_SECRET
    amazon.bucket: YOUR_BUCKET
  5. And that should be it. From your code, you can access the bucket by using:
    $this->get('knp.gaufrette.filesystem_map')->get('amazon');
    
    Copy from http://www.greg-gilbert.com/2013/05/configuring-symfony-2-for-gaufrette-and-amazon-s3/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s