Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images disappear on Heroku #324

Closed
canedy opened this issue Sep 6, 2017 · 4 comments
Closed

Images disappear on Heroku #324

canedy opened this issue Sep 6, 2017 · 4 comments
Labels

Comments

@canedy
Copy link

canedy commented Sep 6, 2017

I'm having an issue where my images disappear on Heroku. I notice that they disappear after my account goes dormant. I'm currently running on hobby mode. If seems like the image is loaded to memory. When I look at Fae_images I still see the image name. I also notice that when I go into the form the name of the image is gone and when I upload again it creates a new record in Fae_images.

I'm wondering if this is the case that images are stored in memory when they are uploaded from your form. I'm attempting to figure out why images disappear after some period of time and how I can fix this bad behavior.

thanks.

@cpitkin
Copy link
Contributor

cpitkin commented Sep 6, 2017

@canedy Heroku doesn't support long term image storage on dynos as per their documentation here.

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

You will need to setup your application to storage assets on something like AWS S3 or GCP storage. Heroku gives a good tutorial on how to setup S3.

@canedy
Copy link
Author

canedy commented Sep 6, 2017

Thanks for the information. Complicates things for me, but I understand your response. If anyone else has other ways to solve let me know, but will move down the path to AWS. If anyone has any lesson learned on this please let me know thanks.

@LeChin
Copy link

LeChin commented Sep 6, 2017

Hi @canedy

I had to work through a similar issue when I was staging on Heroku; I used AWS.

I can't quite remember everything, but here's some tips to get you started:

Setup and get credentials:
https://devcenter.heroku.com/articles/s3

Add this to your gemfile:

# s3 access
gem 'fog', require: 'fog/aws'`

Carrierwave setup:
Example

.env example:
Example

Hope this helps to get you started. Good luck!

@tshedor
Copy link
Contributor

tshedor commented Oct 5, 2017

To add for further reference:

  1. Create bucket in S3. Allow read access for everyone
  2. Go to IAM and create a new user. You'll want programmatic access.
  3. On the permissions screen, choose "Attach a policy directly" (or do this after you create the user).
  4. Create a policy with the following. Be sure to change BUCKET_NAME to the bucket name you created in 1.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1453440321000",
            "Effect": "Allow",
            "Action": [
                "s3:DeleteObject",
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:ListBucket",
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::BUCKET_NAME/*",
                "arn:aws:s3:::BUCKET_NAME"
            ]
        }
    ]
}
  1. Copy your keys and set them appropriately on Heroku: heroku config:set AWS_ACCESS_KEY_ID=<whatever> AWS_SECRET_ACCESS_KEY=<whatever, the sequel> AWS_S3_BUCKET=<BUCKET_NAME>
  2. Add @LeChin 's Carrierwave file to your Rails app:
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  if Rails.env.production?
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region: 'us-west-2', # Change this unless your bucket is in Oregon
      path_style: true
    }
    config.storage :fog
    config.fog_directory = ENV['AWS_S3_BUCKET']
  else
    config.storage :file
  end
end
  1. Add @LeChin 's gem (gem 'fog', require: 'fog/aws')
  2. Bundle, commit, push to Heroku.
  3. Profit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants