如何使用S3(AWS SDK v3)缓存LiipImagineBundle生成的图像

时间:2015-08-03 14:20:32

标签: php symfony amazon-s3 dependency-injection liipimaginebundle

我正在尝试使用LiipImagineBundle的S3缓存解析器将我的缓存缩略图上传到S3,但我遇到了一些奇怪的问题。

我正在使用AWS SDK v ^ 3.2:

"aws/aws-sdk-php": "^3.2",

我正在使用Symfony2,在我的services.yml中,我已经配置了Aws\Credentials\Credentials类和使用它来创建客户端的Aws\S3\S3Client

shq.amazon.s3Credentials:
    class: Aws\Credentials\Credentials
    arguments: ["%amazon.s3.key%", "%amazon.s3.secret%"]

shq.amazon.s3:
    class: Aws\S3\S3Client
    arguments:
        - version: %amazon.s3.version%
          region: %amazon.s3.region%
          credentials: "@shq.amazon.s3Credentials"

一切正常,我可以将文件上传到S3。

现在,LiipImagineBundle的配置似乎LiipImagineBundle的配置规则仅适用于AWS SDK 2,并且使用aws_s3缓存导致“误报”解析器。

我尝试过至少三种不同的配置。

配置1:适用于AWS SDK 2

liip_imagine:
    resolvers:
        default:
            web_path: ~
        cache_s3:
           aws_s3:
               client_config:
                    key:    %amazon.s3.key%
                    secret: %amazon.s3.secret%
                    region: %amazon.s3.region%
                    version: %amazon.s3.version%
               bucket: %amazon.s3.bucket%
               get_options:
                   Scheme: 'https'
               put_options:
                   CacheControl: 'max-age=86400'

    cache: cache_s3

    filter_sets:
        thumb_purchase:
            filters:
                thumbnail: { size: [250, 250], mode: outbound }
                interlace:
                    mode: line

这会引发异常:

  

在渲染模板期间抛出了异常   (“从实例配置文件元数据中检索凭据时出错”   服务器。 (cURL错误28:连接在1006毫秒后超时   (见http://curl.haxx.se/libcurl/c/libcurl-errors.html))“)in   第135行的src / AppBundle / Resources / views / Store / show.html.twig。

这是由searching for credentials的AWS SDK引发的,但未找到它们。

所以,这种方法似乎不起作用。

配置2:使用Aws\Credentials(AWS SDK 3)

将凭据传递给S3Client的方法是传递Aws\Credentials的实例。我通过服务这样做,所以我可以在其他服务中重用相同的凭据(这就像我用它来上传文件到S3一样!):

shq.amazon.s3Credentials:
    class: Aws\Credentials\Credentials
    arguments: ["%amazon.s3.key%", "%amazon.s3.secret%"]

并在config.yml中:

liip_imagine:
    resolvers:
       cache_thumb_purchase:
           aws_s3:
               client_config:
                    version: %amazon.s3.version%
                    region: %amazon.s3.region%
                    credentials: "@shq.amazon.s3Credentials"
               bucket: %amazon.s3.bucket%
               get_options:
                   Scheme: 'https'
               put_options:
                   CacheControl: 'max-age=86400'
    filter_sets:
        thumb_purchase:
            cache: cache_thumb_purchase
            filters:
                thumbnail: { size: [250, 250], mode: outbound } # Transforms 50x40 to 32x26, no cropping
                interlace:
                    # mode can be one of none,line,plane,partition
                    mode: line

如您所见,我将Credentials服务传递给LiipImagineBundle,但我从Aws\ClientResolver收到此错误:

  

ClientResolver.php第296行中的InvalidArgumentException:   为“凭据”提供的配置值无效。预期的Aws \ Credentials \ CredentialsInterface | array | bool | callable,但得到字符串'@shq.amazon.s3Credentials'(length = 25)   

     

凭据:(Aws \ Credentials \ CredentialsInterface | array | bool | callable)

     

指定用于签署请求的凭据。提供一个   Aws \ Credentials \ CredentialsInterface对象,一个关键数组“key”,   “secret”和可选的“token”键,false使用null凭据,或   用于创建凭据或返回null的可调用凭证提供程序。   有关内置凭据的列表,请参阅Aws \ Credentials \ CredentialProvider   供应商。如果未提供凭据,SDK将尝试加载   他们来自环境。

似乎,它不是传递Credentials引用的类@shq.amazon.s3Credentials,而是简单地传递字符串@shq.amazon.s3Credentials,因此ClientResolver正确地报告传递的参数不正确。

因此,此配置也不起作用。

配置3:将阵列传递到S3Client(AWS SDK 3)

实例化S3Client的第三种方式是passing an associative array to hardcode credentials

所以我尝试了这个配置:

liip_imagine:
    resolvers:
       cache_thumb_purchase:
           aws_s3:
               client_config:
                    version: %amazon.s3.version%
                    region: %amazon.s3.region%
                    #credentials: "@shq.amazon.s3Credentials"
                    credentials:
                        key: %amazon.s3.key%
                        secret: %amazon.s3.secret%
    ...

但这次我收到来自LiipImagineBundle的错误,似乎不接受数组作为参数:

  

ScalarNode.php第36行中的InvalidTypeException:路径的类型无效   “liip_imagine.resolvers.cache_thumb_purchase.aws_s3.client_config.HERE_THERE_IS_DIRECTLY_MY_AWS_KEY”。   预期的标量,但得到阵列。

我认为捆绑包的配置规则必须更新,但我不知道如何做到这一点,因为我对这类事情还不太自信。

那么,如何让LiipImagineBundle / Symfony传递类而不是引用Credentials类的字符串?是我的错,还是在LiipImagineBundle

中出现了问题

(另外,我不理解的奇怪的事情,而不是在路径中使用术语“键”,它使用“{”的值HERE_THERE_IS_DIRECTLY_MY_AWS_KEY。 所以它会报告像liip_imagine.resolvers.cache_thumb_purchase.aws_s3.client_config.HERE_THERE_IS_DIRECTLY_MY_AWS_KEY这样的路径,而不是liip_imagine.resolvers.cache_thumb_purchase.aws_s3.client_config.key这样的路径 - 这对我来说似乎更正确,不是吗?)。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

liip_imagine:
    resolvers:
        cache_thumb_purchase:
            aws_s3:
                client_config:
                    credentials:
                        key:    %amazon.s3.key%
                        secret: %amazon.s3.secret%
                    version: %amazon.s3.version%
                    region: %amazon.s3.region%
                bucket: %amazon.s3.bucket%
                get_options:
                    Scheme: 'https'
                put_options:
                    CacheControl: 'max-age=86400'
    filter_sets:
        thumb_purchase:
            cache: cache_thumb_purchase
            filters:
                thumbnail: { size: [250, 250], mode: outbound } # Transforms 50x40 to 32x26, no cropping
                interlace:
                    # mode can be one of none,line,plane,partition
                    mode: line


services:
    admin.amazon.s3:
        class: Aws\S3\S3Client
        factory_class: Aws\S3\S3Client
        factory_method: 'factory'
        arguments:
            - key: %amazon.s3.key%
              secret: %amazon.s3.secret%
              region: %amazon.s3.region%
              version: %amazon.s3.version%

我现在开始处理此LiipImagineBundle + KnpGaufretteBundle + "aws/aws-sdk-php": "^3.18"