I'm trying to build an app where users can upload a profile picture. I'm trying to use Paperclip and S3.
I've got it working on my local machine, but for some reason it doesn't work on Heroku. Specifically, nothing is getting uploaded to S3, but there are also no errors being produced in the Heroku logs from Paperclip or S3. Here's what I'm seeing in the logs:
2012-10-02T07:22:33+00:00 app[web.1]: [paperclip] Saving attachments.
2012-10-02T07:22:33+00:00 app[web.1]: [paperclip] saving profile_pictures/2/original.jpg
2012-10-02T07:22:33+00:00 app[web.1]: [paperclip] saving profile_pictures/2/thumb.jpg
2012-10-02T07:22:33+00:00 app[web.1]: [paperclip] saving profile_pictures/2/medium.jpg
2012-10-02T07:22:33+00:00 app[web.1]: [paperclip] saving profile_pictures/2/large.jpg
Here's what my s3.yml file looks like:
development:
access_key_id: XXXXXXX
secret_access_key: XXXXXXX
bucket: dev-bucket
test:
access_key_id: XXXXXXX
secret_access_key: XXXXXXX
bucket: test-bucket
production:
access_key_id: XXXXXXX
secret_access_key: XXXXXXX
bucket: prod-bucket
Anyone have an idea why this works on my dev machine but not on Heroku? I'm perplexed by the lack of an error...
Did you set the environment variables using Heroku Toolbelt?
$ heroku config:set AWS_BUCKET=**BUCKET NAME**
$ heroku config:set AWS_ACCESS_KEY_ID=***GET FROM AMAZON AWS***
$ heroku config:set AWS_SECRET_ACCESS_KEY=***GET FROM AMAZON AWS***
For this to work you would have to set:
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']
}
}
Related
I am getting Aws::S3::Errors::AccessDenied Access Denied whenever I tries to upload an image using paperclip and aws-sdk-s3.
I have almost tried all the solution available on Internet.
I have tried configuring paperclip default option from here
Gems used
gem "paperclip", git: "git://github.com/thoughtbot/paperclip.git" and gem 'aws-sdk-s3', '~> 1'
My development.rb file configuration looks like
config.paperclip_defaults = {
storage: :s3,
path: '/:class/:attachment/:id_partition/:style/:filename',
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
s3_region: ENV['AWS_REGION'],
},
bucket: ENV['AWS_BUCKET'],
}
My model.rb file looks like
has_attached_file :photo
has_attached_file :image
has_attached_file :signature
My paperclip.rb file looks like
Paperclip::Attachment.default_options[:s3_protocol] = 'http'
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
My aws.yml file looks like
development:
access_key_id: MY_AWS_ACCESS_KEY_ID
secret_access_key: MY_AWS_SECRET_ACCESS_KEY
I am using gem 'figaro' to save my environment variable for development environment.
May if someone can assists me would be appreciated.
The reason for me getting Aws::S3::Errors::AccessDenied Access Denied was my canned ACL properties were not configured properly.
Thanks, It might help someone.
Rais application deployed in aws elastic beantalk. Images are not loading from assets folder. I am trying to use s3 bucket to store assets. I got an error...
aws.yml
production:
access_key_id: 123333231331....
secret_access_key: 12212dddddd........
production.rb
config.paperclip_defaults = {
:storage => :s3,
:preserve_files => true,
:s3_credentials => 'aws.yml',
:s3_region => 'ap-south-1',
:s3_host_name => 's3.ap-south-1.amazonaws.com',
:bucket => 'xxxxxx'
}
I also give public access permissions in s3 bucket.
anyone: read write
gem...
gem 'aws-sdk', '~> 2.10', '>= 2.10.85'
aws.yml folder within config folder -- config/aws.yml
Have you tried using the path to the file, as at the bottom of your question: :s3_credentials => 'config/aws.yml'.
Otherwise, you might need to explicitly load the file from YAML and pass this in:
require 'yaml'
...
# again, perhaps using config/aws.yml, have a play
:s3_credentials => YAML.load_file('aws.yml')
...
Either of those help?
I am at the final stage of deployment of my web app, and am trying to get S3 storage working. Basically, an admin user can log into the site, create a new "opportunity" using rails admin and then upload a picture using paperclip in rails admin. However when you go to save the new "opportunity" it doesn't work. I have been following this tutorial:
https://devcenter.heroku.com/articles/paperclip-s3
And my project is on
https://github.com/jamescook106/do_one_thing
Please excuse my messy commits!
This is what my production.rb file has in:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
I'm trying to fetch some files from my S3 bucket to my Rails 3 Application and stream them to the browser
In my Gemfile
gem 'aws-s3', :require => 'aws/s3'
and I also have configured my s3.yml
development:
bucket: my_unique_bucket
access_key_id: my_key
secret_access_key: my_super_key
test:
bucket: my_unique_bucket
access_key_id: my_key
secret_access_key: my_super_key
production:
bucket: my_unique_bucket
access_key_id: my_key
secret_access_key: my_super_key
Then in my controller I try to find the file inside the containing folder in the bucket
s3File = S3Object.find "My.pdf","PDFs"
but in the browser I get
AWS::S3::NoConnectionEstablished
Make sure you're establishing the connection to Amazon before your request.
Ex.
AWS::S3::Base.establish_connection!(
:access_key_id => 'ID',
:secret_access_key => 'KEY'
)
I have a rails app running on Heroku. I am using paperclip for some simple image uploads for user avatars and some other things, I have S3 set as my backend and everything seems to be working fine except when trying to push to S3 I get the following error:
The AWS Access Key Id you provided does not exist in our records.
Thinking I mis-pasted my access key and secret key, I tried again, still no luck. Thinking maybe it was just a buggy key I deactivated it and generated a new one. Still no luck.
Now for both keys I have used the S3 browser app on OS X and have been able to connect to each and view my current buckets and add/delete buckets. Is there something I should be looking out for? I have my application's S3 and paperclip setup like so
development:
bucket: (unique name)
access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']
test:
bucket: (unique name)
access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']
production:
bucket: (unique_name)
access_key_id: ENV['S3_KEY']
secret_access_key: ENV['S3_SECRET']
has_attached_file :cover,
:styles => {
:thumb => "50x50"
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":class/:id/:style/:filename"
EDIT NOTE: The ENV['S3_KEY'] and ENV['S3_SECRET'] are environment variables in heroku which i have tried even using my keys directly and it still doesn't work
Note: I just added the (unique name) bits, those aren't actually there--I have also verified bucket names, but I don't even think this is getting that far. I also have my heroku environment vars setup correctly and have them setup on dev
You aren't setting a bucket. It's in your s3.yml file, but you aren't reading that value from your call to has_attached_file.
Paperclip S3 docs:
http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3#s3_protocol-instance_method
Also, pay attention to those people who are telling you not to use a s3.yml file with Heroku. It's a waste and just added abstraction that buys you nothing. You already have your ENV set up with the values you need, so use them.
I've done this before where I don't want to push an s3.yml file to Heroku, but I do want to use one for test and development. In an initializer you can do something like this:
# If an s3.yml file exists, use the key, secret key, and bucket values from there.
# Otherwise, pull them from the environment.
if File.exists?("#{Rails.root}/config/s3.yml")
s3_config = YAML.load_file("#{Rails.root}/config/s3.yml")
S3[:key] = s3_config[Rails.env]['key']
S3[:secret] = s3_config[Rails.env]['secret']
S3[:bucket] = s3_config[Rails.env]['bucket']
else
S3[:key] = ENV['S3_KEY']
S3[:secret] = ENV['S3_SECRET']
S3[:bucket] = ENV['S3_BUCKET']
end
Then when you're setting up Paperclip in your model, you reference the value like this:
...
:s3_credentials => {
:access_key_id => S3[:key],
:secret_access_key => S3[:secret]
},
:bucket => S3[:bucket]
Obviously, this means that you do not want to have your s3.yml file in your git repository (which really, you shouldn't anyway).
I kept getting the same AWS::S3::InvalidAccessKeyId error, and had a very similar s3.yml file. As x1a4 recommended, I used ERB in my yaml file and it worked. Here's what it looks like now:
# myapp/config/s3.yml
development: &DEFAULTS
bucket: myapp_dev
access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>
test:
<<: *DEFAULTS
bucket: myapp_test
production:
<<: *DEFAULTS
bucket: myapp
staging:
<<: *DEFAULTS
bucket: myapp_staging
I guess this might a tad too indirect for some folks, but it seemed like the cleanest implementation to me.
Your s3 yaml file is actually using the strings ENV['S3_KEY'] and ENV['S3_SECRET'] as the auth info for s3. They are not being evaluated as ruby code.
There are a couple of things at least that you can do outside of putting that actual info into the yaml file. You can look into enabling ERB in your yaml configs or just not use a yaml file for your credentials at all, because you're always pulling from the environment in every one of your rails_envs, so the yaml file is just an extra layer of indirection in your case that is useless.