代码之家  ›  专栏  ›  技术社区  ›  antpaw

如何将自定义服务添加到ActiveStorage

  •  5
  • antpaw  · 技术社区  · 7 年前

    我想将自定义服务添加到ActiveStorage,因为我想覆盖 url 方法 ActiveStorage::Service::S3Service 服务,这样我就可以在S3存储桶前面使用CloudFront CDN。我想我不需要 presigned_url 参数,我只需要密钥,因为CloudFront实例将具有对S3 bucket的完全读取权限。

    2 回复  |  直到 7 年前
        1
  •  5
  •   George Claghorn    7 年前

    ActiveStorage::Service ActiveStorage::Service::S3Service

    # lib/active_storage/service/cloudfront_s3_service.rb 
    require "active_storage/service/s3_service"
    
    class ActiveStorage::Service::CloudfrontS3Service < ActiveStorage::Service::S3Service
      def url(key, **)
        # ...
      end
    end
    

    config/storage.yml

    production:
      service: CloudfrontS3
      access_key_id: ""
      secret_access_key: ""
      region: ""
      bucket: ""
    
        2
  •  1
  •   Paul Danelli    7 年前

    S3Service

    require "active_storage/service/disk_service"
    
    # S3 uses a flat folder structure, so mimic that so we an sync files and databases
    module ActiveStorage
      class Service::FlatDiskService < Service::DiskService
        private
    
        def folder_for(key)
          "/"
        end
      end
    end
    

    local:
       service: FlatDisk
       root: <%= Rails.root.join("storage") %>
    
    推荐文章