0

it's my first Rails project with the Refile gem, so I don't fully grasp all the ins and outs of this wonderfull file upload gem yet.

I'm trying to configure the Refile gem so that files are uploaded to Amazon S3 in production mode, but in development mode files are uploaded to the file system.

my config/initializers/refile.rb file looks like this:

require "refile/backend/s3"

aws = {
  access_key_id: Rails.application.secrets.s3_key, 
  secret_access_key: Rails.application.secrets.s3_secret, 
  bucket: "adlit",
}
Refile.cache = Refile::Backend::S3.new(prefix: "cache", **aws)
Refile.store = Refile::Backend::S3.new(prefix: "store", **aws)
end

but when I try to upload an image in development, I get this error message:

Missing Credentials. Unable to find AWS credentials. You can configure your     AWS credentials a few different ways: * Call AWS.config with :access_key_id and     :secret_access_key * Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV *...

So I tried to fix this by adding the configuration in a conditional:

if Rails.env.production
  ...
end

But that didn't fix it, I still get the above error message in development mode.

Does anyone know how I can configure Refile to upload file to Amazon S3 in production and to the file system when in development mode?

thanks for your help,

Anthony

Toontje
  • 1,415
  • 4
  • 25
  • 43

2 Answers2

0

I have done pretty much exactly this and it works fine for me.

I've noticed you've got a small typo, you're missing the ? on the env check. It should be:

Rails.env.production?

Does that fix your problem?

i2w
  • 118
  • 2
  • 6
  • Hi i2w, yeah that works for me. However in production my whole app is crashing. Don't know yet what's causing this, is it a problem of credentials? Not sure yet. – Toontje Feb 25 '15 at 12:51
  • @Toontje, I notice a _huge_ performance issue with refile and S3. I have a product support page that lists a bunch of links to files stored on S3, and it always times out—it takes _forever_ to render each download link and CloudFront gives up waiting for my app. I used to think it was because I was checking to see if the actual file exists before rendering the link, but after removing that check, the problem is still there, so now I think it may have more to do with the `attachment_url` method. Depending on what your app is doing, that may be why it's crashing. – partydrone Oct 23 '15 at 16:07
0

I think you can put all that config code for Amazon S3 in config/environments/production.rb.

user3754535
  • 131
  • 11