AWS SDK v2 for s3
Asked Answered
A

3

5

Can any one provide me a good documentation for uploading files to S3 using asw-sdk Version 2. I checked out the main doc and in v1 we used to do like

s3 = AWS::S3.new
obj = s3.buckets['my-bucket']

Now in v2 when I try as

s3 = Aws::S3::Client.new

am ending up with

Aws::Errors::MissingRegionError: missing region; use :region option or export region name to ENV['AWS_REGION']

Can anyone help me with this?

Aultman answered 3/3, 2015 at 5:55 Comment(1)
Here is the documentation : docs.aws.amazon.com/sdkforruby/api/index.html, You should find everything that you'd need.Fagan
G
3

As per official documentation:

To use the Ruby SDK, you must configure a region and credentials.

Therefore,

s3 = Aws::S3::Client.new(region:'us-west-2')

Alternatively, a default region can be loaded from one of the following locations:

Aws.config[:region]
ENV['AWS_REGION']
Galilean answered 3/3, 2015 at 5:58 Comment(3)
My problem here is accessing buckets. Previously we have a method to access it s3.buckets['my-bucket']. It looks like changed in version2.Aultman
it should be s3.bucket['my-bucket']Galilean
Yeah i tried that as well but ending up with no method error. 5] pry(main)> s3 = Aws::S3::Client.new(region:'us-west-2') => #<Aws::S3::Client> [8] pry(main)> s3.bucket[ENV['AWS_BUCKET']] NoMethodError: undefined method bucket' for #<Aws::S3::Client>`Aultman
S
3

Here's a complete S3 demo on aws v2 gem that worked for me:

Aws.config.update(
  region: 'us-east-1',
  credentials: Aws::Credentials.new(
    Figaro.env.s3_access_key_id,
    Figaro.env.s3_secret_access_key
  )
)
s3 = Aws::S3::Client.new
resp = s3.list_buckets
puts resp.buckets.map(&:name)

Gist

Official list of AWS region IDs here.

If you're unsure of the region, the best guess would be US Standard, which has the ID us-east-1 for config purposes, as shown above.

Scend answered 27/4, 2015 at 17:38 Comment(0)
M
1

If you were using a aws.yml file for your credentials in Rails, you might want to create a file config/initializers/aws.rb with the following content:

filename = File.expand_path(File.join(Rails.root, "config", "aws.yml"))
config = YAML.load_file(filename)
aws_config = config[Rails.env.to_s].symbolize_keys

Aws.config.update({
                  region: aws_config[:region],
                  credentials: Aws::Credentials.new(aws_config[:access_key_id], aws_config[:secret_access_key])
              })

The config/aws.yml file would need to be adapter to include the region.

development: &development
  region: 'your region'
  access_key_id: 'your access key'
  secret_access_key: 'your secret access key'
production:
  <<: *development
Merchandise answered 20/10, 2015 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.