Reputation: 18873
I am trying to upload and display images from an S3 bucket to my rails 4 app on heroku. I want to add an image when creating 'Designer' object
I am using paperclip. In config/environments/production.rb
:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
I set the config variables
$ heroku config
AWS_ACCESS_KEY_ID: AKIAI...HVWD2YQ
AWS_BUCKET: myafricastyle
AWS_SECRET_ACCESS_KEY: VFgFVF4.....gKnT5AMFtP4
This is the model I want to add images to:
class Designer < ActiveRecord::Base
has_many :items, dependent: :destroy
has_attached_file :avatar, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
end
I have a migration to add the images to the Designer table..
class AddAttachmentAvatarToDesigners < ActiveRecord::Migration
def self.up
change_table :designers do |t|
t.attachment :avatar
end
end
def self.down
drop_attached_file :designers, :avatar
end
end
When I try to add the Designer with an uploaded photo I get:
$ heroku logs
[paperclip] saving /designers/avatars/000/000/016/original/moto_ninjas87.jpg
[paperclip] saving /designers/avatars/000/000/016/original/moto_ninjas87.jpg
Completed 500 Internal Server Error in 2622ms
Completed 500 Internal Server Error in 2622ms
app/controllers/designers_controller.rb:33:in `block in create'
app/controllers/designers_controller.rb:33:in `block in create'
app/controllers/designers_controller.rb:32:in `create'
ArgumentError (missing required :bucket option):
ArgumentError (missing required :bucket option):
app/controllers/designers_controller.rb:32:in `create'
What's wrong with my bucket option configuration?
addition:
my controllers/designers_controller.rb
shows:
def create
@designer = Designer.new(designer_params)
respond_to do |format|
if @designer.save
format.html { redirect_to @designer, notice: 'Designer was successfully created.' }
format.json { render action: 'show', status: :created, location: @designer }
else
format.html { render action: 'new' }
format.json { render json: @designer.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_designer
@designer = Designer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def designer_params
params.require(:designer).permit(:name, :country, :about, :avatar)
end
I have both the aws-sdk
and paperclip
gems installed
Upvotes: 2
Views: 803
Reputation: 2865
You are missing the bucket in the hash that is passed to s3_credentials. Also you should consider moving config to yml file and load it from there.
:storage => :s3,
:path => "/archive/:style/:id/:filename",
:s3_credentials => File.join(Rails.root, 'config', 's3.yml')
s3.yml file
production:
bucket: <%= ENV['S3_BUCKET'] %>
access_key_id: <%= ENV['S3_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['S3_SECRET_ACCESS_KEY'] %>
Upvotes: 1
Reputation: 394
The following happens because you are calling @designer.save in the create method, and paperclip adds a post-create hook to your designer class.
ArgumentError (missing required :bucket option):
app/controllers/designers_controller.rb:32:in `create'
To fix it, I think that
:bucket => ENV['AWS_BUCKET']
belongs one level up in the hash. So:
config.paperclip_defaults = {
:storage => :s3,
:bucket => ENV['AWS_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
For more info, see: http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3
Upvotes: 0