Images disappear on Heroku
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.
@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.
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.
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!
To add for further reference:
- Create bucket in S3. Allow read access for everyone
- Go to IAM and create a new user. You'll want programmatic access.
- On the permissions screen, choose "Attach a policy directly" (or do this after you create the user).
- Create a policy with the following. Be sure to change
BUCKET_NAMEto 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"
]
}
]
}
- 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> - 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
- Add @LeChin 's gem (
gem 'fog', require: 'fog/aws') - Bundle, commit, push to Heroku.
- Profit.