Rails Paperclip S3 ArgumentError (missing required

2020-03-28 04:35发布

问题:

I've been stuck on this for ages now and can't figure out what's wrong. There are a lot of people that seem to have this same problem, but I can't actually find any answers that actually work.

production.rb

  config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['my bucket name is here'],
    :access_key_id => ENV['my key is here'],
    :secret_access_key => ENV['my secret key is here']
  }
}

game.rb

require 'aws/s3'

class Game < ActiveRecord::Base

attr_accessible  :swf, :swf_file_name, :name, :description, :category, :age_group, :dimension_x, :dimension_y, :image, :image_file_name, :feature_image, :feature_image_file_name, :developer, :instructions, :date_to_go_live, :date_to_show_countdown, :plays
has_attached_file :swf

has_attached_file :image

has_attached_file :feature_image

  def swfupload_file=(data)
    data.content_type =
MIME::Types.type_for(data.original_filename).first.content_type
    logger.warn("Data content type is: #{data.content_type}")
    self.file = data
  end

end

paperclip.rb

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'

回答1:

Here is my paperclip initialization stuff:

Paperclip::Attachment.default_options.merge!({
    storage: :s3,
    s3_credentials: {
        access_key_id: ENV['S3_KEY'],
        secret_access_key: ENV['S3_SECRET'],
        bucket: "#{ENV['S3_BUCKET']}-#{Rails.env}"
        },
    url: ":s3_domain_url",
    path: "/:class/:attachment/:id_partition/:style/:filename"
    })

This assumes that we have three environment variables setup called, you guessed it... S3_KEY, S3_SECRET, and S3_BUCKET. I did a little trick so that I could have a different bucket in each environment by adding Rails.env to the bucket variable.

You seem to indicate in your question that you're putting the actual name of the bucket in the reference to ENV, which would not work. You should put the name of the bucket in the environment variable and use the name of the environment variable as the key.

I hope this helps.