![](/img/trans.png)
[英]Rails direct upload to Amazon S3 using Activeadmin + Paperclip
[英]rails 4 paperclip upload to amazon s3 not working
我一直在關注railstutorial.org上的rails 4教程。 我已經完成了大部分工作,該項目托管在heroku上,但現在想要將圖像上傳到Amazon S3。 我已經關注了heroku網站上的指南,但是在S3(歐洲)上無法上傳到我的桶。
我正在使用paperclip 3.5.2。
發布模型
has_attached_file :post_photo,
styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
},
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET'] },
:s3_protocol => "https",
:path => ":class/:id/:basename_:style.:extension",
:url => ':s3_eu_url',
:bucket => 'bucket_name'
后控制器
def post_params
params.require(:post).permit(:post_photo, :user_username, :title, :comment, :location, :user_id)
end
配置/初始化/ Paperclip.rb
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
"#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}
到config / environment.rb
require 'aws/s3'
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"
配置/環境/ production.rb
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
:url => ':s3_eu_url',
:path => ":class/:id/:basename_:style.:extension"
}
所以,我已經得到了這個工作(不是歐洲S3,但這不重要)在config/environments/production.rb
沒有任何東西 - 主要是因為我使用ENV變量來幫助控制我指向哪個桶at,API密鑰等
這是我的配置:
在config/environments/production.rb
:
只有標准配置 - 與回形針無關。
在config/initializers/paperclip.rb
:
Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_protocol] = 'http'
Paperclip::Attachment.default_options[:s3_credentials] =
{ :bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] }
對於上述內容,您需要添加:
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
然后,您的Post
模型應該只需要具有以下內容:
has_attached_file :post_photo,
styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
這可能很明顯,但也要確保你的Gemfile中加載了aws-sdk
gem。
如果您有疑問,請告訴我。 我已經設置了很多次,完全有助於排除故障。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.