如何将自定义服务添加到ActiveStorage [英] How to add a custom service to ActiveStorage
问题描述
我想向ActiveStorage添加自定义服务,因为我想覆盖
I want to add a custom service to ActiveStorage because I want to overwrite the url
method of the ActiveStorage::Service::S3Service service so I can use a CloudFront CDN in front of my S3 bucket. I think I will not need the presigned_url
params, I will just need the key, because the CloudFront instance will have full read access to the S3 bucket.
推荐答案
在 ActiveStorage :: Service
命名空间中添加一个对 ActiveStorage :: Service :: S3Service
进行子类化的类.覆盖要覆盖的方法.例如:
Add a class in the ActiveStorage::Service
namespace that subclasses ActiveStorage::Service::S3Service
. Override the methods you want to override. For example:
# 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
中引用您的自定义服务:
Refer to your custom service in config/storage.yml
:
production:
service: CloudfrontS3
access_key_id: ""
secret_access_key: ""
region: ""
bucket: ""
这篇关于如何将自定义服务添加到ActiveStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!