mardi 12 janvier 2016

Refile Gem Image Links Break When I Push a New Commit to Heroku

My application is using the following gems:

gem "refile", require: "refile/rails"
gem "refile-s3"
gem "refile-mini_magick"

What my application does is that it uploads image files to an AWS S3 instance, then gets retrieved using helper methods from the Refile gem.

Everything works as expected when I run it locally.

The problem is when I push my app to Heroku, I can upload images and it displays correctly. However, when I push a newer commit to Heroku, all previous images' links break.

I've checked that the file_id of the image on the page is the same file_id of the image on the Heroku database.

This is my upload form for my images:

  <div class="field">
    <%= f.label :images_files %><br>
    <%= f.attachment_field :images_files, multiple: true, presigned: true, direct: true %>
  </div>

This is a snippet of my image being shown:

<%= attachment_image_tag(project.images.first, :file, :fill, 750, 400) %>

I have set my environment variable correctly on Heroku so that it knows the keys to my AWS instance (access key id, secret access key, and bucket name).

This is the accessor to upload to S3:

# config/initializers/refile.rb
require "refile/s3"

aws = {
  access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  region: "us-west-1",
  bucket: ENV['export S3_BUCKET'],
}

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

Another thing I've noticed was that my S3 bucket didn't hold the images in its correct folder if I uploaded through my Heroku app. However, if I uploaded an image using the local server I can see the image in my S3 bucket. I'm not sure why because all the environment variables are correct and I've double-checked it.

Aucun commentaire:

Enregistrer un commentaire