[英]Issue with Paperclip/S3 in Rails 3.2.5 “cannot load such file — aws-sdk”
[英]How to access Rails / Paperclip S3 assets in show view, aws-sdk 2
使用新的AWS Ruby SDK(aws-sdk 2.2.18)和Paperclip,我設法將音頻文件上傳到S3。
我發現的所有教程和文檔均參考aws-sdk的1.x版本,例如: Heroku教程
到目前為止,這是我的代碼:
模型
has_attached_file :audio
validates_attachment_content_type :audio, :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
編輯表格(simple_form)
<%= f.input :audio, as: :file %>
控制者
def update
if @sentence.update(sentence_params)
s3_bucket = Aws::S3::Resource.new.bucket(Rails.application.secrets.aws_s3_bucket)
s3_filename = get_s3_filename(@sentence)
s3_obj = s3_bucket.object(s3_filename)
s3_obj.put(body: sentence_params[:audio])
redirect_to @sentence, notice: 'Sentence was successfully updated.'
else
render :edit
end
end
我遇到的問題是,當我嘗試訪問顯示視圖中的鏈接以播放音頻時,它沒有指向S3。
顯示視圖
<%= link_to @sentence.audio.url %>
渲染
/system/sentences/audios/000/408/166/original/sample_audio.mp3?1456335849
如何在S3中訪問資產,並專門播放音頻?
好像您在has_attached_file行中缺少s3_credentials選項。
has_attached_file :download,
:storage => :s3,
:s3_credentials => Proc.new{|a| a.instance.s3_credentials }
Paperclip文檔中提供了更多信息。
正確設置s3連接后,您將不需要更新方法中包含的任何代碼。
呼喚
<%= link_to @sentence.audio.url %>
會自動返回指向存儲在S3存儲桶中的文件的正確URL。
希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.