![](/img/trans.png)
[英]Rails 6 Active storage sidekiq file upload background image on S3
[英]Rails Active Storage upload to a public S3 remote url
我需要通過 API 將視頻上傳到第 3 方。 我使用 API 請求了這個有效期為 15 分鍾的“上傳位置”。 現在我需要將我的 Active Storage 視頻直接上傳到這個遠程上傳位置。 這個偏遠的位置不是由我管理的。
我已閱讀官方文檔,但不清楚在哪里可以使用此更改默認上傳位置 url。 文檔: https : //edgeguides.rubyonrails.org/active_storage_overview.html#direct-uploads
上傳位置:
{"uploadLocation"=>"https://storage-3rd-party.s3.eu-west-1.amazonaws.com/staging/folderr/12345/xxxx?X-Amz-
Security-Token=xxx...."}
如果我理解正確,您將要為這個 3rd 方 API 實現一個自定義的ActiveStorage::Service 。 在幕后, rails 調用url_for_direct_upload
來獲取您想要自定義的 URL。
如果您實現了這樣的新服務,您應該能夠接近工作:
class ThirdPartyStorageService < ActiveStorage::Service
def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
ThirdPartyAPI.get_upload_location(...)
end
# Implement other abstract methods...
end
然后,您需要在config/storage.yml
添加您的服務:
third_party:
service: ThirdPartyStorageService
# username: ...
# password: ...
# other config...
然后您可以將其設置為用於特定模型或全局。
# app/models/my_model.rb
class MyModel
has_one_attached :file, service: :third_party
end
# or config/application.rb
config.active_storage.service = :third_party
這有點工作,但我認為這應該讓你成功! 請務必閱讀ActiveStorage::Service 上的文檔,如果您不確定如何實現某種方法,可以查看 Azure、AWS 和 Google 存儲服務的實現以獲取靈感。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.