“missing required :bucket option” for Paperclip/S3

前端 未结 3 1175
深忆病人
深忆病人 2021-02-05 22:40

In my Rails app I\'m letting users upload an image when they create a \"release\", and it should upload directly to S3. I\'m getting the following error in both development and

相关标签:
3条回答
  • 2021-02-05 22:45

    I think that's because :bucket should be an option passed to Paperclip not to S3.
    Fixed config

      config.paperclip_defaults = {
        :storage => :s3,
        :s3_protocol => 'http',
        :bucket => ENV['AWS_BUCKET'],
        :s3_credentials => {
          :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
        }
      }
    

    And Paperclip::Storage::S3 doc seems to confirm that, even being so poorly written/formatted.

    EDIT:

    In one of my projects I use Paperclip with Fog gem and this works well

    Paperclip::Attachment.default_options.merge!(
      :storage => :fog,
      :fog_credentials => {
        :provider => 'AWS',
        :aws_access_key_id => ENV['S3_ACCESS_KEY_ID'],
        :aws_secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
        :region => 'eu-west-1' # in case you need it
      },
      :fog_directory => ENV['S3_BUCKET'], # only one of those is needed but I don't remember which
      :bucket => ENV['S3_BUCKET']
    )
    
    0 讨论(0)
  • 2021-02-05 22:45

    Add this to your application.rb file inside the module and class. create a local_env.yml file and put your environment variables in there. This code will load your environment variables on server start :

    config.autoload_paths += %W(#{config.root}/lib)
    config.before_configuration do
        env_file = File.join(Rails.root, 'config', 'local_env.yml')
        YAML.load(File.open(env_file)).each do |key, value|
            ENV[key.to_s] = value
        end if File.exists?(env_file)
    end
    
    0 讨论(0)
  • 2021-02-05 23:11

    In my case it was that I was using foreman (Heroku) which uses an .env file to store environment variables. So, when I did rake db:migrate it couldn't find the ENV['AWS_ACCESS_KEY_ID']

    What I did to run my migration was I temporarily added my AWS credentials directlñy into Carrierwave config block and then removed them after...

    This is not a permanent solution because next time you migrate it will say the same thing...

    For the permanent solution see: Use environment variables in Rake task

    which says use: foreman run rake some_task this way all variables defined in .env are loaded for the rake task too

    0 讨论(0)
提交回复
热议问题