Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I host images in heroku? Or do I need S3?

I'm deploying my web app (it's for a corporate client). So, users will not add images, but only the business will.

I've deployed to Heroku, and my images are still showing. When do I need to use S3? Ill have like 100 images in total in the site, and size will vary like > 7 a week. Can I use only heroku?

like image 639
Gibson Avatar asked Jul 08 '14 23:07

Gibson


People also ask

Does Heroku support image upload?

Image uploading and resizing with a prebuilt S3 setup. Free cloud storage.

Does Heroku provide file storage?

Heroku has an “ephemeral” hard drive, this means that you can write files to disk, but those files will not persist after the application is restarted. By default Active Storage uses a :local storage option, which uses the local file system to store any uploaded files.

What can you host in Heroku?

Many of Heroku's add-on partners provide limited free cloud hosting solutions for popular data stores, such as Redis, MongoDB, MySQL, and Neo4j Graph.

Is hosting on Heroku safe?

Heroku is not secure enough. But Heroku is not secure! LOL. Unless you're in a heavily regulated industry, like finance, or you require a particular certification that is not supported by Heroku, this should not be an issue.


2 Answers

The short answer: if you allow users or admins to upload images, you should not use Heroku's file system for this as the images will suddenly vanish.

As explained in the Heroku documentation:

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.

This means that user uploaded images on the Heroku filesystem are not only wiped out with every push, but also with every dyno restart, which occasionally happens (even if you would ping them frequently to prevent them going to sleep).

Once you start using a second web dyno, it will not be able to read the other dyno's filesystem, so then images would only be visible from one dyno. This would cause weird issues where users can sometimes see images and sometimes they don't.

That said, you can temporarily store images on the Heroku filesystem if you implement a pass-through file upload to an external file store.

like image 75
fivedigit Avatar answered Oct 06 '22 00:10

fivedigit


Asset Pipeline

FiveDigit's answer is very good - there is something more to consider; the role of the asset pipeline in Rails

If the images you have are used as assets (IE they are used in the layout; are not changeable by the user), then you can store them in the assets/images folder. There is no limit to the number of assets you can keep with your application, but you must be sure on what these are - they are files which aid your application's operation; not files which can be uploaded / manipulated:

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB.

The asset pipeline will compress & fingerprint the stylesheet, image and js files it has, when you deploy your application to the likes of Heroku, or any other server. This means if those files don't change, you can store them in there

-

S3

The reason you'd want to use the likes of S3 is specifically if your images files are designed to change (user can upload / edit them). Regardless of Heroku's filesystem, if the images are tied to changes in the DB, you'll have to keep a central store for them - if you change servers, they need to be reachable

To do this, you should ensure you appreciate how you want the files to work - are they going to be manipulated constantly by the user or not? If so, you'll have to explore integrating S3 into your app

like image 39
Richard Peck Avatar answered Oct 06 '22 00:10

Richard Peck