1

I am using the latest version of the refile gem to upload images to AWS and it's working fine. when I try to test my app with rspec i get this error:

/aws-sdk-core/plugins/regional_endpoint.rb:34:in `after_initialize': missing region; use :region option or export region name to ENV['AWS_REGION'] (Aws::Errors::MissingRegionError)

Gemfile:

gem "refile", require: "refile/rails"

gem "refile-mini_magick"

gem "refile-s3"

refile.rb

require 'refile/simple_form'

require "refile/s3"

aws = {

access_key_id: ENV['AWS_ACCESS_KEY_ID'],

secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],

region: ENV['AWS_REGION'],

bucket: ENV['AWS_BUCKET']

}

Refile.cache = Refile::S3.new(prefix: "cache", **aws)

Refile.store = Refile::S3.new(prefix: "store", **aws)

I tried setting a new initializer aws.rb:

require 'aws-sdk'

Aws.config.update({ region: 'us-west-2', credentials: Aws::Credentials.new('akid', 'secret') })

but it did not work.

10x for your help!

2 Answers2

1

found the answer: just add to your initializers/refile.rb:

require "refile/s3"
require 'refile/simple_form'

if Rails.env.production?
  aws = {
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
    region: ENV['AWS_REGION'],
    bucket: ENV['AWS_BUCKET']
  }

  Refile.cache = Refile::S3.new(prefix: "cache", **aws)
  Refile.store = Refile::S3.new(prefix: "store", **aws)
end
0

It looks like your code is looking for the AWS_REGION value as an environment variable. Have you verified that the value for AWS_REGION is being set in your environment before running your tests? You can see if it's set in bash by doing the following:

env | grep AWS_REGION

If it's not set, then just need to set the variable like so (again in bash):

export AWS_REGION="us-west-2"
Kel Cecil
  • 174
  • 4
  • The errors are happening when the OP is running tests ... you generally should avoid having your tests access the network, it slows things down and may cause unwanted side effects. So you probably don't want to define the missing environment variable, and instead want to disable the Refile backends in the rails initializer as shown in the other answer ;) – Sunil D. May 27 '20 at 16:43