After updating images for many products on HerokuApp, I came back some minutes later to see that all images of the products but two are missing?
Is this normal or is this fixable?
Thanks!
Heroku doesn't allow you to save images to it due to its ephemereal storage unless they're a part of your assets folder and precompiled. For uploading images you need to tie it into AmazonS3 (or some other image store) because only the database data persists. Here's a heroku dev guide that shows how to use AmazonS3 to store images and why they can't be saved on Heroku's platform itself:
https://devcenter.heroku.com/articles/active-storage-on-heroku
Related
Any idea how to use Carrierwave to upload images with Heroku.
I added this to the uploader file:
def cache_dir
"#{Rails.root}/tmp/uploads"
end
but images still don't save! After uploading an image, it saves and once you refresh the page, the image breaks.
Any help would be appreciated! Thanks
I do not think you can use Heroku and upload images without 3rd party storage service like Amazon S3.
https://devcenter.heroku.com/articles/s3
Heroku allows you store files inside tmp but just in order to send to a 3rd party service. Inside carrierwave uploader class you can set for example
storage :fog
instead of default :file and setup uploads to AWS S3. There are other options as well.
One thing is that if you are using a free tier instance on Heroku your upload needs to finish in abut a minute - I would recommend setup where you upload files directly to s3 account.
https://github.com/dwilkie/carrierwave_direct
Hope it helps
The filesystem on Heroku is not persisted. Only files uploaded through deployment mechanisms (git push) are "persisted". Others like the ones in your "#{Rails.root}/tmp/uploads" folder will be erased. That's why they are disappearing.
I have answered a similar question here. Here is a quote:
Your dyno on Heroku has a "read-only" filesystem. In a sense, your files will not be persisted between your dyno restarts and there is no guarantee that they will persist between any two requests. Here is an excerpt from the docs:
Each dyno has its own ephemeral file system, not shared with any other dyno, that is discarded as soon as you disconnect. This file system is populated with the slug archive so one-off dynos can make full use of anything deployed in the application.
You can use the #{Rails.root}/tmp folder as temporary folder, but you need to upload your files to some external storage (S3, some CDN, etc.). Heroku has some addons that makes it easy to handle.
I uploaded my app to heroku. The app was built with ruby on rails and had the activeadmin and paperclip gem installed. The files are configured properly and i was able to upload images using he active admin gem. this worked perfectly loaclly and worked fine on heroku until i uploaded more images, now all the images have disappeared. Any ideas on how this can be sorted?
Heroku has an ephemeral filesystem.
That is because each running dyno is an independent container (much like docker) which shared nothing with other dynos, and is destroyed when the app is restarted/deployed.
So any file written on disk will be lost when the dyno is restarted, and is not recoverable.
You need to configure paperclip to upload images to a dedicated file storage system like Amazon S3.
You can't store images on heroku, or anything for that matter. Your uploaded files are available for temporary use only, long enough to process them away to cloud storage elsewhere.
http://cloudinary.com/ offers development accounts for free; https://aws.amazon.com/s3/ has become ridiculously cheap and gives new devs a long free trial. There are other resources but ultimately you have to choose storage which isn't transient.
You can use gems like carrierwave, or attachinary to easily access and store with either of those storage locations.
I am using carrierwave for image uploads. I am storing the images at /public/uploads
When I deploy to heroku, images that were uploaded and saved previously are wiped out.
How can this be avoided?
Turns out I can't upload images to heroku.
I have decided to use cloudinary for the time being.
I will probably switch to S3 eventually.
I'm using Carrierwave to upload images, and my app is on Heroku right now.
Images are uploaded successfully unless I push a new commit to heroku. Images that I uploaded before a push seem to be erased when a new commit comes in. Does anyone know the reason behind and how to fix this issue?
Update:
The problem becomes, using carrierwave on Heroku without a storage server like Amazon EC3. Heroku does not save files in public folder, where carrierwave uploads by default.
app/uploaders/image_uploader.rb:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
And you need to change this to somewhere in the tmp folder in order to preserve your image files after each commit. I tried to change it to
"#{Rails.root}/tmp/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
But it does not work. I checked on my local environment, and the image is stored in the right location successfully. So I guess if I just set the routes to the right location, this could work. Could someone help me with this?
Since Heroku does not allow storing static files (unless it's associated the application itself), you should make Carrierwave upload to a remote repository (e.g. Amazon S3) using fog. Everytime a user uploads a file, the file will be automatically uploaded to your S3 storage instead of Heroku.
I'm using Rails 3 with assets pipeline enabled.
And I know images such as icons or logos should go in app/assets/images.
But where should I put images that may be a lot and big? For example images uploaded by the users, such as photos
I don't want them to be duplicated by precompile (not sure if it does that, I don't know how it works). And I want them to be ready to use as soon as they get uploaded.
Any ideas?
The image_tag wasn't working as expected. In the documentation it says 'By default, files are loaded from public/images', but that wasn't working.
The solution was to add /images to the path (with leading backslash, or it won't work)
<%= image_tag '/images/some_dir/some_image.jpeg' %>
Amazon S3 is a great, inexpensive way to store images that your users upload. I recommend using the Paperclip gem which integrates with s3. There's a section in the paperclip documentation with instructions on how to use s3. No clue what precompiling images means or how it applies to uploaded image files, but they will be available as soon the upload is complete. Using this also has some additional benefits such as acting as a backup system.