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

ant*_*paw 6 ruby-on-rails amazon-web-services rails-activestorage

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

Geo*_*orn 6

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
Run Code Online (Sandbox Code Playgroud)

请参阅以下内容中的定制服务config/storage.yml

production:
  service: CloudfrontS3
  access_key_id: ""
  secret_access_key: ""
  region: ""
  bucket: ""
Run Code Online (Sandbox Code Playgroud)

  • @PRWhitehead,大多数现代文件系统不限制单个目录中的文件数量,但有些文件系统会遇到包含超过 30,000 个文件的目录的性能问题,这使删除前缀、系统维护、备份等变得复杂。 (2认同)