I am integrating paperclip with S3 bucket in RAILS 5. I am referring https://coderwall.com/p/vv1iwg/set-up-ruby-on-rails-with-paperclip-5-and-s3-using-aws-sdk-v2.
I have written the following in my development.rb:
config.paperclip_defaults = {
storage: :s3,
s3_region: 'us-west-2',
s3_credentials: {
bucket: 'mybucket',
access_key_id: 'my id',
secret_access_key: 'my secret key'
}
}
I am getting the below error while uploading image:
NameError (uninitialized constant Aws::VERSION):
I am using version 3.0.1 of gem aws-sdk
Instead of downgrading, you can introduce the variable that paperclip is missing in the current version. Adding this file works with aws-sdk 3.0.1 and paperclip 5.1.0, and will probably work until paperclip is updated to accommodate the aws changes.
# config/initializers/aws.rb
Aws::VERSION = Gem.loaded_specs["aws-sdk"].version
I have downgraded the aws-sdk version to aws-sdk (2.10.42) and it works.
Here is my config for CarrierWave with minimagick, I think something similar can be done for your case:
config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['S3_ACCESS_KEY'],
aws_secret_access_key: ENV['S3_SECRET_KEY'],
region: ENV['S3_REGION']
}
config.fog_directory = ENV['S3_BUCKET']
end
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.
I have this Rails 3.2 application passed on from the previous freelancer in my company. I would like to know as to how to configure this existing S3 bucket to my new EC2 Instance.
Access to AWS is configured in fog.yml by using fog gem as well as carrierwave gem.
What am I missing in this fog.yml file
Fog.credentials_path = Rails.root.join('config/fog.yml')
CarrierWave.configure do |config|
config.fog_credentials = {
}
config.fog_directory = "directory-name"
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
#config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'aws_key',
:aws_secret_access_key => 'aws_secret'
}
config.fog_directory = 'bucket1'
config.fog_host = 'https://s3.amazonaws.com'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
#app/uploader/image_uploader.rb
def store_dir
"images/#{model.class.to_s.underscore}"
end
#app/views/pictures/show.html.erb
<%= image_tag #picture.image_url if #picture.image? %>`enter code here`
I'd take a look at these examples:
http://fog.io/about/getting_started.html
It looks like the format is basically:
development:
aws_access_key_id: 'XXXXXXXXXXXXXXX'
aws_secret_access_key: 'XXXXXXXXXXXXXXX'
provider: 'AWS'
test:
aws_access_key_id: 'XXXXXXXXXXXXXXX'
aws_secret_access_key: 'XXXXXXXXXXXXXXX'
provider: 'AWS'
# set these environment variables on your prod server
production:
aws_access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
aws_secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
provider: 'AWS'
Make sure to use spaces rather than tabs since it's a YAML file.
I'm trying to setup Figaro in Rails 4, but after setting up the correctly yaml as such
application.yml
aws_access_key_id:'#'
aws_secret_access_key:'#'
fog_directory:'#'
CarrierWave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS", # required
aws_access_key_id: ENV["aws_access_key_id"], # required
aws_secret_access_key: ENV["aws_secret_access_key"], # required
}
config.fog_directory = ENV["fog_directory"] # required
end
I keep getting this error
`global_configuration': undefined method `reject' for #<String:0x007f9c7a0d9a80> (NoMethodError)
I've looked into similar problems but haven't found the right answer, the YAML worked on http://www.yamllint.com/
The yaml file is super sensitive and even though it passed on http://www.yamllint.com/, there was a formatting error it didn't catch
I'm using carriervawe and fog with S3 bucket. I get the error in the title in development (when I run rails s or rake db:migrate) with the following code:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.asset_host = "http://xxx.cloudfront.net"
config.fog_directory = 'xxx'
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
config.storage = :fog
end
I've also tried using (as suggested here)
<%= ENV['AWS_ACCESS_KEY_ID'] %>
but I get this error:
syntax error, unexpected '<' (SyntaxError)
My variables are in application.yml file
AWS_ACCESS_KEY_ID: AKIAIxxx...
AWS_SECRET_ACCESS_KEY: 1xxx...
Not sure why, but for some reason your environment variables are likely being evaluated to nil. I like to use the figaro gem to manage my environment variables.
Simply add
gem "figaro"
to your gemfile.
Then run
figaro install
which will create an application.yml file and add it to your .gitignore (very important for security reasons). After this, you should be able to add your AWS keys to application.yml and access them in your carrierwave config like your currently have.
If this is from the Michael Hartl tutorial, I solved my issues by renaming the initializer to carrierwave.rb instead of carrier_wave.rb, as suggested in the tutorial. I then re-ran the git and Heroku commands and it worked on the Heroku production server.
I just pulled an older version of a project I'm working on in rails from github and am having problems with carrierwave for image upload. I used the figaro gem to store my secret keys so they were not in the file I pulled down (figaro puts an application.yml file that then gets listed in .gitignor). So I added the figaro configuration, but carrierwave still refuses to work. I even tried putting the keys into the carrierwave configuration directly to see if it was something with figaro, but no luck.
My config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_KEY'],
:region => 'us-east-1',
}
config.fog_directory = 'bucketname'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
I am pretty sure that my keys are being stored properly in my development environment, but I have no idea why carrierwave is not working like it did before.
Thanks!