Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Development error using paperclip and Amazon s3

I have my app running on heroku and have configured to save all assets uploaded to a bucket using Amazon s3.That all works fine. So when i tried to upload an image locally (development) i get the following error

AWS::S3::Errors::PermanentRedirect in RecipesController#update 
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

My update action

 def update
 @recipe = Recipe.find(params[:id])

 if @recipe.update_attributes(params[:recipe])
  redirect_to my_recipes_path, :notice => "Successfully updated recipe"
 else 
 render :action => 'edit'

end
end

Though after some reading it appears it is because I am using a bucket in the EU (not the default US one)

I have two buckets, one for development and one for production. and have created a s3.yml file to hold the credentials, though I think it would be better to use ENV variables, I use Ubuntu and I could update my .bashrc file? not to sure on that one.. Anyway my s3.yml file (actual keys removed for security obviosuly)

development:
access_key_id: KEY
secret_access_key: KEY
bucket: assets.recipesappdev

production:
access_key_id: KEY
secret_access_key: KEY
bucket: assets.recipesapp

and my recipe model config

has_attached_file :avatar, 
:styles => { :myrecipes => "260x180#", :showrecipe => "600x300#", :medium => "300x300>", :thumb => "100x100>" },
 :storage => :s3,
 :s3_credentials => "#{Rails.root}/config/s3.yml",
  :path => "/images/:id/:style.:extension"

Has anyone got a fix for this, I have tried this for example http://www.conandalton.net/2011/02/paperclip-s3-and-european-buckets.html but that doesnt work, though my initializer may be wrong, i tried configuring to suit my app

  Paperclip.interpolates(:s3_eu_url) { |assets, style|
 "#{assets.s3_protocol}://s3-eu-west-1.amazonaws.com/#{assets.recipesappdev}  /#{assets.path(style).gsub(%r{^/}, "")}"
   }
like image 222
Richlewis Avatar asked Dec 01 '22 22:12

Richlewis


2 Answers

For the ones, like me, the url solution is not working try to set the s3_host_name option:

:s3_host_name => "s3-eu-west-1.amazonaws.com"

Looks like the european nodes have this issue.

Taken from here

like image 31
fguillen Avatar answered Jan 20 '23 06:01

fguillen


Try to set the url option on has_attached_file to ":s3_domain_url". (Don't forgot to surround with quotes.)

has_attached_file :avatar, 
                  :styles => { :myrecipes => "260x180#", :showrecipe => "600x300#", :medium => "300x300>", :thumb => "100x100>" },
                  :storage => :s3,
                  :s3_credentials => "#{Rails.root}/config/s3.yml",
                  :path => "/images/:id/:style.:extension",
                  :url => ":s3_domain_url"

See the RDoc for Paperclip::Storage::S3.

like image 72
Chris Peters Avatar answered Jan 20 '23 05:01

Chris Peters