我有兩個服務定義在 storage.yml
amazon:
service: S3
bucket: bucket1
region: eu-central-1
access_key_id: 321
secret_access_key: 321
unsafe_files:
service: S3
bucket: unsafe-files
region: eu-central-1
access_key_id: 123
secret_access_key: 123
我對某些檔案使用了 amazon 服務,我想unsafe_files對其他檔案使用該服務,以便我可以將它們放入另一個s3存盤桶中。
使用該unsafe_files服務的模型看起來像這樣(非常簡單):
class Customer < ActiveRecord::Base
belongs_to :customer_image
end
class CustomerImage < Image
has_one_attached :file, service: Rails.configuration.settings[:unsafe_files_service]
end
Rails.configuration.settings[:unsafe_files_service] 只是 unsafe_files
在導軌中application.rb我正在設定
config.active_storage.service = :amazon以便默認情況下它使用amazon定義的服務storage.yml
現在我想直接使用 上傳檔案form_with,這也很簡單:
<%= form_with model: [@customer, CustomerImage.new] do |form| %>
<%=
form.file_field :file,
accept: 'image/jpeg',
direct_upload: true,
multipart: true
%>
<%= form.submit "submit" %>
<% end %>
現在我知道(或者我想我知道)使用active_storage. 一些javascript監聽表單提交事件,首先獲取其中的內容file_field并將其發送到DirectUploadsController檢查影像的,生成直接上傳的url,然后我們直接將檔案上傳到指定的服務。這一切都在這里:https : //github.com/rails/rails/blob/6ecf1065da57360bdc9f1d85e2c2d9314dcb79e0/activestorage/app/controllers/active_storage/direct_uploads_controller.rb#L14
該服務使用檔案密鑰或 ID 進行回應。之后表單提交繼續,我們保存我們收到的id,這就是檔案之間的關系。
But when we hit the DirectUploadsController and we create the blob it gets the default service_name (see: https://github.com/rails/rails/blob/6ecf1065da57360bdc9f1d85e2c2d9314dcb79e0/activestorage/app/models/active_storage/blob.rb#L115)
So long story short, when using direct upload I can't choose a service I have to rely on rails default service.
Is there a workaround for that? Or maybe I have missed something?
uj5u.com熱心網友回復:
如果您想給create_before_direct_upload!自己打電話(以便您可以指定 service_name),那么我認為您必須DirectUploadsController以某種方式覆寫。您可以撰寫自己的控制器,或者(高級)嘗試重新打開并修補DirectUploadsController. 我的應用程式是前者。
來自 OP 的更新:
我最終重新打開了 DirectUploadsController(并且還在表單 file_field 中添加了一個選項以傳遞自定義的 direct_upload_url 它作業得很好,雖然我不喜歡我不得不與內部人員打交道太多。我已經檢查了 active_storage gem,它似乎那個人將不得不添加這個功能,因為當前的實作沒有解決方法。- beniutek
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/348329.html
標籤:ruby-on-rails ruby ruby-on-rails-6 rails-activestorage
上一篇:RubyTK如何接收用戶的輸入?
下一篇:Ruby控制臺-帶退出的多行命令
