I am using latest aws-sdk for Ruby. My rake file contains following line to connect to S3 bucket:
require 'aws-sdk'
...
AWS.config(
:access_key_id => 'xxxxx',
:secret_access_key => 'xxxxx'
)
s3 = AWS::S3.new
object = s3.buckets['bucket_in_ireland'].objects['data.csv']
When accessing an AWS S3 bucket in region US that works perfectly fine. But getting following error while accessing AWS S3 bucket in region Ireland:
AWS::S3::Errorrs::PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
From Google I could find two solutions:
Put AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com" at the bottom of environment.rb
Set :s3_host_name => 's3-eu-west-1.amazonaws.com' for paperclip default settings
When I placed the line mentioned in (1) at the bottom of environment.rb, the error message changed to following:
NameError: uninitialized constant AWS::S3::DEFAULT_HOST
Then I tried (2) by updating following code in production.rb but that didn't help.
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
}
Note: Also tried to apply this fix [https://github.com/aws/aws-sdk-ruby/issues/510] but nothing really changed after modifications in environment.rb, but nothing work.
Any help please...
Setting s3_endpoint to AWS config solved the issue for me.
AWS.config(
:access_key_id => 'xxxx',
:secret_access_key => 'xxxx',
:s3_endpoint => 's3-eu-west-1.amazonaws.com'
)
Related
I'm having trouble with loading images uploaded to s3 via my cloudfront distribution. I can get it to work fine loading straight from s3, but not from cloudfront.
I have set the config.action_controller.asset_host to my distribution url and have set up paperclip
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-sa-east-1.amazonaws.com',
:url => ':asset_host',
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
with the appropriate code in the model :path =>
':class/:id/:style/:class:style:id.:extension'
and every time I upload a file and try to access it i get
https://XXXXXX.cloudfront.net/images/:class/:id/:style/:class:style:id.:extension
I honestly can't figure out where the /images/ is coming from that is added between the url and the path. And I would be very greatful for any help or tips to solve this!
Thanks
I have a rails 4 app deployed on heroku. I'm using Amazon s3 bucket to host images (using paperclip gem), but when I try to upload an image to my articles controller i get this error:
Permission denied # dir_s_mkdir - /articles
I have gone through the steps listed here. As i understand this could be a folder permission issue, but not really to sure how to tackle the issue.
Try putting the following code in your /config/environments/production.rb file and update ENV secret key names accordingly. The host name is dependent on your region, us-west-1, eu-west-1, etc.
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:s3_host_name => 's3-us-west-1.amazonaws.com'
}
}
I have tried many different ways to set up S3 using ENV variables for image uploads and cannot get it to work. I know my keys and bucket name work, because when I put them straight into the code, my images upload correctly. However, when I try to switch to ENV variables, things do not work.
I used the figaro gem, which created application.yml. In that file, I have:
S3_BUCKET_NAME "xxxxx"
AWS_ACCESS_KEY_ID: "AAAAAAAAA"
AWS_SECRET_ACCESS_KEY: "BBBbbbBBBB"
Not sure if there should be any quotation marks there or not, but right now, I have them in. I have tried without, also.
In my model (listing.rb), I have:
has_attached_file :image,
:styles => { :medium => "200x" , :thumb => "100x100" },
:default_url => "default.png",
:storage => :s3,
:s3_credentials => Proc.new{|a| a.instance.s3_credentials }
def s3_credentials
{:bucket => ENV["S3_BUCKET_NAME"], :access_key_id => ENV["AWS_ACCESS_KEY_ID"],
:secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
end
Like I said, when I hard code the values into def s3_credentials, everything works fine. It's just when I try to swap out ENV variables that things fall apart.
In paperclip.rb, I have:
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
I also have this code in both production.rb and development.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Here is the error message I get when uploading a new image: "The request signature we calculated does not match the signature you provided. Check your key and signing method." On line: "if #listing.save". The ones that were uploaded w/ the credentials hard-coded are still able to be seen in my app.
I'm fairly new to rails and have looked here and other places, including the S3 and paperclip docs, and cannot find a solution that will work. Please let me know if you need to see any other code. I plan on deploying to heroku, if that matters, and saw that figaro is supposed to play nicely with heroku. THANK YOU.
EDIT/UPDATE: For anyone else reading this in the future, Sachin's answer below worked. However, there was a '+' in one of my key IDs. When trying to added the ENV variable via command line, all the characters after the '+' (and including it) were cut off. Simply wrap them in "", and you should be good to go.
Also, I dropped use of the figaro gem, and set up an aws.rb initializer file (per Amazon's instructions). Here are the contents of that file:
AWS.config(
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)
S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]
And I don't know if this made any difference, but my development.rb and production.rb files now have the following as paperclip defaults:
config.paperclip_defaults = {
:storage => :s3,
:bucket => "your_real_bucket_name_here_in_quotes",
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Also note the switch to referring to the ENV bucket name as S3_BUCKET vs. S3_BUCKET_NAME.
And the code in my model (listing.rb) is now this:
has_attached_file :image, :styles => { :medium => "200x", :thumb "100x100"}, :default_url => "default.png", :storage => :s3, :bucket => "your_real_bucket_name_here_in_quotes"
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
You can do one thing:
You can set this configuration in your development.rb or production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
And If you want to set this environment variables into local then use this:
sudo nano ~/.profile
Then add your variables over here
export S3_BUCKET_NAME="your bucket name"
export AWS_ACCESS_KEY_ID="your access key id"
export AWS_SECRET_ACCESS_KEY="your secret access key"
And then reload your ~/.profile with . ~/.profile
Check added variable with echo $S3_BUCKET_NAME
And for Heroku
You can set your variable like:
heroku config:set S3_BUCKET_NAME="your bucket name"
heroku config:set AWS_ACCESS_KEY_ID="your access key id"
heroku config:set AWS_SECRET_ACCESS_KEY="your secret access key"
Check that variables added or not in heroku with heroku config
For more detail you can refer form here.
Let me know if you need me more..
Able to upload files to Amazon s3 in my production environment with Heroku, but unable to do this in my development environment. Here is how the situation looks currently
Development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
After following the advice of this S.O. Post, I created a file application.yml in my config directory with the following code:
AWS_ACCESS_KEY_ID: "***"
AWS_SECRET_ACCESS_KEY: "***"
AWS_BUCKET: "themoderntrunk"
I didn't change my design model though
has_attached_file :photo, :styles => { :thumbnail => "80x80#",
:small => "150x150>"
}
When I'm uploading file's now, i get this error
missing required :bucket option
Many people I've seen had this same problem, but none of the answers have been able to solve my problem. Any guidance would be truly appreciated. Thanks.
Allegorically, this particular issue has commonly been resolved using Fog, Ruby's canonical cloud services library.
# Gemfile
gem 'fog'
Run bundle install, then modify your configuration file as follows:
# config/environments/development.rb
Paperclip::Attachment.default_options.merge!(
:storage => :fog,
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:fog_directory => ENV['AWS_BUCKET'],
:bucket => ENV['AWS_BUCKET']
)
Restart your server to reload the environment – uploads should work.
UPDATE:
Upon a rereading of your question, I noticed that you're to passing an environment global named ENV['S3_BUCKET_NAME'], but the variable you should actually pass is named ENV['AWS_BUCKET']. I suspect this is why the missing required :bucket option was being thrown. Renaming the variable may resolve your original issue:
# config/environments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'], # Matches global declaration in `application.yml`
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
I'm trying to set this up using the article on herokus developer site.
https://devcenter.heroku.com/articles/paperclip-s3
# 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'],
:url => 'https://s3-ap-southeast-2.amazonaws.com'
}
}
However all of my images are pointing to the us http://s3.amazonaws.com/ server
How can i set paperclip to use the sydney server?
UPDATE:
paperclip successfully uploads files to the sydney server, it just wrongly uses the US server rather than the sydney one when trying to view them
Paperclip provides a handful of options, to optimize the configuration of your bucket.
The ones you're interested are url and s3_alias_url.
Your configuration would look like something like this :
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'],
},
:url => ':s3_alias_url',
:s3_alias_url => 's3-ap-southeast-2.amazonaws.com'
}
You can find more information and an other example in this google groups discussion.