Trouble accessing methods in aws/s3 gem - ruby-on-rails

Hi I'm playing around with the aws/s3 gem so that my rails app can store and then download files from Amazon s3. I'm finding that I can't access the methods contained within the gem.
I followed the instructions in the documentation:
Entered into irb
required 'aws/s3'
entered interactive shell provided by aws/s3: % s3sh
AWS::S3::Base.establish_connection!(
:access_key_id => 'my credentials',
:secret_access_key => 'my credentials'
)
From here I believe I should be able to access my buckets and objects within them but when I call Service.buckets I get an error that states undefined method 'buckets'.
I also tried (not using s3sh):
service = AWS::S3::Base.establish_connection!(
:access_key_id => 'my credentials',
:secret_access_key => 'my credentials'
)
then service.buckets but still I get undefined method 'buckets'. How do I use this gem correctly?
Any help appreciated, thanks a lot.

AWS::S3::Service.buckets listed the buckets.

Related

images uploaded to aws, but can't be viewed in the view

I have just integrated AWS with my rails/heroku app and I am using paperclip. I am able to upload files (photo's) and see them in AWS, however they are not showing up in the view. I am not getting any errors, and have not found a working solution in other posts.
It seems I am able to view the image in a browser, and that permissions are set to public:
I suspect that I may have my region wrong, in the url of my aws dashboard the region says region=us-west-2 yet googling and reading through other forums and posts on SO leads me to believe that if I am in the US my region should be set to us-east-1 - currently I have it set to the latter. Not sure if this is the problem.
Here is a link to the image on AWS:https://s3.amazonaws.com/giving-tree-images/avatars/1/medium/02108_navajoland_1440x900.jpg
here is the code pertaining to aws/paperclip in my model:
:storage => :s3,
# :s3_host_name => "s3-us-east-1.amazonaws.com",
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:default_url => "default_img.png"
and I am displaying in the view like this:
<%= image_tag #user.avatar.url(:medium) %>
I would love it if someone could point me in the right direction... Any additional info needed, please let me know!
I also faced the same issue, so this is how I solved it:
Create a new initializer file i.e. config/initializers/paperclip.rb
Add following in this file
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
To get details about your region, go to aws
Amazon Web Service - Check your host name
Note : Replace s3-us-west-2.amazonaws.com with your own host name
Reference : https://devcenter.heroku.com/articles/paperclip-s3
Have you enable the read permission to everyone?
It gave me access-denied error when accessed to the link.

Getting error The AWS Access Key Id you provided does not exist in our records when I try to delete a file

Gemfile :
gem 'aws-sdk', '< 2.0'
in the initializer folder I created a file aws.rb with the following code
AWS.config(access_key_id: '...key id...', secret_access_key: '...secret_key...')
S3_BUCKET = AWS::S3.new.buckets['my_bucket']
I have a file in the path :
my_backet/uploads/391cd178-a64a-4fda-83b6-210430c34a0c/file.png
when I try to delete a file
def delete_file
key = '391cd178-a64a-4fda-83b6-210430c34a0c/file.png'
S3_BUCKET.objects[key].delete
end
I get the error
AWS::S3::Errors::InvalidAccessKeyId in ImagesController#delete_file
The AWS Access Key Id you provided does not exist in our records.
I was able to solve that by providing an s3_endpoint as parameter to my AWS.config :
AWS.config(
:s3_endpoint => '...',
:access_key_id => '....',
:secret_access_key => '....'
)
The source that helped me : https://ruby.awsblog.com/post/TxVOTODBPHAEP9/Working-with-Regions
Double check the credential initialization syntax and format. It is possible you have not specified them there correctly.
The references below indicate different syntax that the one you are using.
AWS.config(:access_key_id => 'KEY', :secret_access_key => 'SECRET')
Please see:
https://ruby.awsblog.com/blog/tag/config
http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/Core/Configuration.html
http://www.rubydoc.info/gems/aws_sdk/3.1.5/AWS/Core/Configuration

Ruby aws-sdk gem configuration Hash is converted to String

I am trying to use amazon S3 for storing and retrieving files from my rails application, have setup the changes in configuration file for aws_credentials, but what is supposed to by sent as hash to the below configuration.rb file is being sent as string.
Credentials in development.rb in my application is as follows:
:s3_credentials => {
:bucket => "mybucketname",
:access_key_id => "MYACCESSKEYID",
:secret_access_key =>"MYSECRECTACCESSKEYID"
}
The options object supposed to be a hash is being sent as string like this:
"bucket:mybucketname access_key_id:MYACCESSKEYID
secret_access_key:MYSECRECTACCESSKEYID"
That is causing the issue with the below error:
/.rvm/gems/ruby-1.9.3-p484/gems/aws-sdk-v1-1.64.0/lib/aws/core/configuration.rb:292:in `with': undefined method `inject' for (NoMethodError)
The part of error occurence in configuration.rb file in the gem is:
def with options = {}
options = options.inject({}) {|h,kv| h[kv.first.to_sym] = kv.last; h }
values = supplied.merge(options)
if supplied == values
self # nothing changed
else
self.class.new(values.merge(:__created__ => #created.dup))
end
end
I have no idea where the object type getting modified, i even tried using the credentials through yaml file but still the same issue. Its working in my colleague's machine in another project, but somehow for my project i am finding difficult with this issue. Advance thanks for any help.

AWS S3 in rails - how to set the s3_signature_version parameter

I'm trying to set up the Amazon Simple Storage Service for use with rails. I'm getting this error message:
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
The problem is that I chose the Frankfurt S3 region, and there only the V4 scheme is supported.
It's the same error message as in this post, which directs you to the solution
here, with instructions how to "set the :s3_signature_version parameter to :v4 when constructing the client". The command is:
s3 = AWS::S3::Client.new(:s3_signature_version => :v4)
My question is, how do I do this? Where do I put this code?
EDIT:
I tried putting :s3_signature_version => :v4 in carrier_wave.rb as follows, but during the upload to heroku it said [fog][WARNING] Unrecognized arguments: s3_signature_version, and it didn't make any difference, I still get the error.
config/initializers/carrier_wave.rb:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY'],
:s3_signature_version => :v4
}
config.fog_directory = ENV['S3_BUCKET']
end
end
EDIT:
I've created a new bucket using the Northern California region, for which this isn't supposed to be a problem, but I'm still getting exactly the same error message.
EDIT:
This doesn't make any difference either:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY']
}
config.fog_directory = ENV['S3_BUCKET']
config.fog_attributes = {:s3_signature_version => :v4}
end
end
I had the problem, that Spree v2.3 was fixated to aws-sdk v1.27.0. But the parameter s3_signature_version was introduced in v1.31.0 (and set per default for China).
So in my case the following configuration for Frankfurt has totally been ignored:
AWS.config(
region: 'eu-central-1',
s3_signature_version: :v4
)
I found this old question from the other direction, trying to take the advice in https://github.com/fog/fog/issues/3450 and set signature to version 2 (to test a hypothesis). Delving into the source a bit, it turns out the magic phrase is :aws_signature_version => 4, so like this:
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY'],
:aws_signature_version => 4
}
I had this same problem and could not find any guidance on where to implement the s3_signature_version: :v4 command.
In the end, I basically deleted the existing bucket in Frankfurt and created on in the Standard US zone and it works (after updating the permissions policy attached to the user accessing the bucket to reflect that the bucket has changed).
I would love to have the bucket in Frankfurt but I don't have another 16 hours to spend going round in circles with this issue so if anybody is able to add a bit more direction on how to incorporate the s3_signature_version: :v4 line, that would be great.
For other users following Michael Hartl's Rails Tutorial:
you (might*) need at least v 1.26 of the 'fog' gem. Modify your Gemfile accordingly, and don't forget to '$ bundle install'.
*the reason is that some S3 buckets require authorization signature version 4. In the future probably all of them will, and at least Frankfurt (zone eu-central-1) requires v4 authorization.
This has been supported since fog v1.26:
https://github.com/fog/fog/blob/v1.26.0/lib/fog/aws/storage.rb

What is the proper way to set S3 creds in Rails Development ENV

What is the proper way to set S3 credentials in a Rails development enviroment.
I have
`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']
}
}`
in config/environments/development.rb
but would like to know where to actually set S3_BUCKET_NAME, etc
I recommend the figaro gem. As a bonus it works great with Heroku, too.
I store mine in an external file that gets read in on boot, with a fallback using config/env.rb for defaults. These two gems do the trick for me. The latter adds some nice stuff like whining if an ENV var isn't set that you think should be set.
gem 'dotenv-rails'
gem 'env_bang-rails'

Resources