AWS SDK v2 for s3

Can someone provide me with good documentation for uploading files to S3 using asw-sdk Version 2. I checked the main document and in v1 we used how

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

Now in v2 when i try how

 s3 = Aws::S3::Client.new 

am ends

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

Can anyone help me with this?

+5
source share
3 answers

According to official documentation :

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

Thus,

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

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

 Aws.config[:region] ENV['AWS_REGION'] 
+3
source

Here's the full 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

The official list of AWS region identifiers is here.

If you are not sure about the region, the best option would be US Standard, which has the us-east-1 identifier for configuration purposes, as shown above.

+2
source

If you used the aws.yml file for your credentials in Rails, you can create a config/initializers/aws.rb with the following contents:

 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 must be an adapter to enable the region.

 development: &development region: 'your region' access_key_id: 'your access key' secret_access_key: 'your secret access key' production: <<: *development 
+1
source

Source: https://habr.com/ru/post/1214533/


All Articles