簡體   English   中英

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

[英]How to use S3 (AWS SDK v3) to cache images generated by LiipImagineBundle

我正在嘗試使用LiipImagineBundle的S3緩存解析器將緩存的縮略圖上傳到S3,但是我LiipImagineBundle了一些奇怪的問題。

我正在使用AWS開發工具包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開發工具包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/ src / AppBundle / Resources / views / Store / show.html.twig中的libcurl-errors.html ))“)行135。

搜索憑據但未找到憑據的AWS開發工具包會拋出此錯誤。

因此,這種方法似乎行不通。

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

將憑證傳遞給S3Client是傳遞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 |可調用, 但是得到了字符串'@ shq.amazon.s3Credentials'

憑據:(Aws \\ Credentials \\ CredentialsInterface | array | bool |可調用)

指定用於簽署請求的憑據。 提供一個Aws \\ Credentials \\ CredentialsInterface對象,“ key”,“ secret”和一個可選的“ token”密鑰的關聯數組,為false表示使用空憑據,或者提供可調用的憑據提供程序以創建憑據或返回null。 有關內置憑據提供程序的列表,請參見Aws \\ Credentials \\ CredentialProvider。 如果沒有提供憑據,SDK將嘗試從環境中加載它們。

似乎沒有傳遞@shq.amazon.s3Credentials引用的類Credentials ,而是僅傳遞了字符串@shq.amazon.s3Credentials ,因此ClientResolver正確地報告了傳遞的參數不正確。

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

配置3:將數組S3Client (AWS SDK 3)

實例化S3Client的第三種方法是將關聯數組傳遞給硬編碼憑據

所以我嘗試了這種配置:

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某些東西損壞了?

(此外,我不明白的是,它沒有在路徑中使用術語“ key”,而是使用了“ key”的值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之類的路徑-對我來說似乎更正確,不是嗎?)。

你有嘗試過嗎?

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM