Ruby on Rails, Paperclip, Amazon AWS S3 & Heroku

前端 未结 3 1114
-上瘾入骨i
-上瘾入骨i 2020-12-30 16:08

I try for two days to make my all web site work on the internet through Heroku and Amazon AWS S3 ( to store my images ) but ... I can\'t make it !

To make it simpli

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 16:59

    Had this problem before!

    Solved it by putting the bucket vars in the model itself (live code):

     #app/models/image.rb
     has_attached_file :image,
                    :styles => { :medium => "x300", :thumb => "x100" },
                    :default_url => "**********",
                    :storage => :s3,
                    :bucket => '*****',
                    :s3_credentials => S3_CREDENTIALS
    
     #config/application.rb
     config.paperclip_defaults = {
                :storage => :s3,
                :s3_host_name => 's3-eu-west-1.amazonaws.com'
     }
    
     #config/initializers/s3.rb
     if Rails.env == "production"
         # set credentials from ENV hash
     S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "*****"}
     else
         # get credentials from YML file
         S3_CREDENTIALS = Rails.root.join("config/s3.yml")
     end
    
    #config/application.yml ([figaro][1] gem)
    S3_KEY: ********
    S3_SECRET: **********
    

    We also have this in our production.rb:

    #app/environments/production.rb
    config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    

    The ENV['FOG_DIRECTORY'] is the bucket name, and there's also one for different regions. Here's a very good resource for you (the answer with 15 upvotes... not the accepted answer)

提交回复
热议问题