LazyBlobStorage
Useful for attaching small database-backed blobs to records. Good
when you need small file upload support, but don't want to bother with a cloud
service because you know you'll have low traffic/user counts. Uses a SHA256
digest to prevent duplicate blob uploads.
Nothing fancy. Just stores files and allows public access using the
digest as a key. Respects HTTP Cache headers.
You probably want to put a CDN/Cache/RateLimit in front of the
"lazy_blobs#show"
route because somebody could DOS your website
if all your workers are serving files from the database.
Basically this is a dumb idea, but setting up AWS buckets and permissions just
to upload a dumb logo is :big_sigh_emoji:
Don't use this. I mean it.
Alternative
I would use activestorage_database rather than lazy_blob_storage if I were starting a new project.
See:
Breaking Changes in 2.0.0-beta.1
- Support UUID key types
- YOLO support for rails > 6 (it's probably fine)
- Migrations do not get added to rails paths automatically anymore. They must be copied with the
lazy_blob_storage:install:migrations
rake task. - The default max blob size is 100kb. It should probably be lower. You can set this to a number of bytes with
LazyBlobStorage.max_blob_size=
- !!! Do not use this for high traffic projects. !!!
- !!! Do not use this to store big files !!!
- !!! This dumb idea only works for very small files like 10kb logos or icons. !!!
- !!! you were warned !!!
Note
If it's not obvious. This is for inclusion in a Rails app.
Built with Rails 6. It could probably work with Rails 4/5, too.
Usage
- Add
gem "lazy_blob_storage" to Gemfile
- Run
rake lazy_blob_storage_engine:install:migrations
- Run
rake db:migrate
- Declare
has_lazy_attached :image
in some model - Add
f.file_field :image_upload
to the form template - Use
image_tag @thing.lazy_attached_image_path
or image_tag @thing.lazy_image_url
Note: LazyBlobsController respects HTTP cache directives
License
The gem is available as open source under the terms of the MIT License.