arsthanea/remote-media-bundle

3.0 2020-06-05 10:52 UTC

README

此组件允许您将资源存储在S3桶中,而不是本地文件系统中

安装

composer require arsthanea/remote-media-bundle

配置

KunstmaanMediaBundle 之后将此组件添加到您的内核中

…
  $bundles = [
    //
      new Kunstmaan\MediaBundle\KunstmaanMediaBundle(),
      new ArsThanea\RemoteMediaBundle\RemoteMediaBundle(),
    //
  ];
…

config.yml 中设置S3详情

remote_media:
  cdn:
    # The public URL to your bucket. You may use a CDN for instance
    # If not sure leave null
    #
    media_url: https://a-cdn-for-my-bucket.cloudflare.net
    
    # cached thumbnails can be stored in a prefixed path.
    # the default value is the env name (i.e. "dev/") or no prefix on production
    #
    # cache_prefix: dev
    
    # To speed up cache resolution, provide a doctrine_cache provider:
    #
    # cache_provider: liip_imagine

    s3:
      bucket: acme-bundle
      
      # this is optional and defaults to eu-west-1
      #
      # region: eu-west-1
      
      # please provide AWS access key and secret for this bucket:
      #
      access_key: %aws_access_key%
      access_secret: %aws_access_secret%

您需要稍微调整一下 liip_imagine 的配置。在 config.yml 或其他位置找到默认设置,并将 cachedata_provider 键设置为 remote_media

liip_imagine:
  cache: remote_media
  data_loader: remote_media

设置缓存提供者

使用 DoctrineCacheBundle (已被Kunstmaan Bundles使用) 创建缓存提供者

doctrine_cache:
  providers:
    liip_imagine:
      type: redis

用法

使用 media_url twig函数将媒体路径替换为CDN主机。如果您不需要CDN,请注意,现在 Media::getUrl() 返回的是完整URL,而不是相对路径。

  <img src="{{ media_url(resource.image.url) }}">