2
votes

I am attempting to create a Users controller in my ruby on rails project, which I have also configured with heroku and an aws-s3 bucket. I set my .env and my heroku local with the S3_BUCKET, AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY. I also set my initializer/aws.rb file to look like this:

Aws.config.update({
  region: 'us-east-1',
  credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
})

S3_BUCKET = Aws::S3::Resource.new.bucket(ENV['S3_BUCKET'])

I have bundle installed the aws gem like this:

gem 'aws-sdk', '~> 3'

However when I run the command

rails g controller Users new

I get the following error in my terminal:

aws-sdk-s3/bucket.rb:658:in `extract_name': missing required option :name (ArgumentError)

I looked at that file and it is trying to find the S3 bucket name, but I have set that already in .env and heroku local. Is there some other place where this needs to be set? None of the guides I have read mention this error.

2
tell me whether the below code worked or notAniket Shivam Tiwari

2 Answers

3
votes

Hi please check whether you have specified the right credentials and bucket name. Also, make sure you have provided the right region .Try the below code

s3 = Aws::S3::Resource.new(
      credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'],  ENV['AWS_SECRET_ACCESS_KEY']),
      region: 'us-west-1'
    )

    obj = s3.bucket(ENV['S3_BUCKET']).object('key')

If you want to upload file or something

obj.upload_file(file, acl:'public-read')

0
votes

This would help you, I have used like this in my project. 1. Create the file aws.rb in your /config/initializers folder. 2. Then copy the below code,

S3Client = Aws::S3::Client.new(
    access_key_id: 'ACCESS_KEY_ID',
    secret_access_key: 'SECRET_ACCESS_KEY',
    region: 'REGION'
) 

Thats all, this works. Happy coding :)