Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages in full color showing how to combine Symfony with Docker, APIs, queues & async tasks, Webpack, Single-Page Applications, etc.

Buy printed version

Amazon S3 Resolver

1.0 version
Maintained

Amazon S3 Resolver

The AmazonS3Resolver resolver enables cache resolution using Amazon S3.

Dependencies

This cache resolver requires the aws-sdk-php library, which can be installed by executing the following command in your project directory:

1
$ composer require aws/aws-sdk-php

Note

This command requires that Composer is installed globally, as explained in their installation documentation.

Configuration

To begin, you must assign your Amazon key, secret, and bucket to their respective parameters.

1
2
3
4
5
6
# app/config/config.yml or app/config/parameters.yml

parameters:
    amazon_s3.key:    "your-aws-key"
    amazon_s3.secret: "your-aws-secret"
    amazon_s3.bucket: "your-bucket.example.com"

Prerequisites

Next, you must define the required services.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# app/config/services.yml

services:

    acme.imagine.cache.resolver.amazon_s3:
        class: Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver
        arguments:
            - "@acme.amazon_s3"
            - "%amazon_s3.bucket%"
        tags:
            - { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }

    acme.amazon_s3:
        class: AmazonS3
        arguments:
            -
                key:    "%amazon_s3.key%"
                secret: "%amazon_s3.secret%"

Usage

After configuring AmazonS3Resolver, you can set it as the default cache resolver for LiipImagineBundle using the following configuration.

1
2
3
4
# app/config/config.yml

liip_imagine:
    cache: amazon_s3

Usage on a Specific Filter

Alternatively, you can set AmazonS3Resolver as the cache resolver for a specific filter set using the following configuration.

1
2
3
4
5
6
7
8
9
# app/config/config.yml

liip_imagine:
    filter_sets:
        cache: ~
        my_thumb:
            cache: amazon_s3
            filters:
                # the filter list

Tip

If you want to use other buckets for other images, simply alter the parameter names and create additional services.

Object URL Options

In order to make use of the object URL options, you can simply add a call to the service, to alter those options you need.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# app/config/services.yml

services:
    acme.imagine.cache.resolver.amazon_s3:
        class: Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver
        arguments:
            - "@acme.amazon_s3"
            - "%amazon_s3.bucket%"
        calls:
             # This calls $service->setObjectUrlOption('https', true);
             - [ setObjectUrlOption, [ 'https', true ] ]
        tags:
            - { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }

You can also use the constructor of the resolver to directly inject multiple options.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# app/config/services.yml

services:
    acme.imagine.cache.resolver.amazon_s3:
        class: Liip\ImagineBundle\Imagine\Cache\Resolver\AmazonS3Resolver
        arguments:
            - "@acme.amazon_s3"
            - "%amazon_s3.bucket%"
            - "public-read" # AmazonS3::ACL_PUBLIC (default)
            - { https: true, torrent: true }
        tags:
            - { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.