Unofficial Cloudflare Images Gem
Unofficial gem to integrate Cloudflare Images with Rails Active Storage. It behaves like a normal Active Storage service but stores files in Cloudflare Images and serves them via Cloudflare’s optimized CDN.
About Cloudflare Images
Cloudflare Images is a service for storing, optimizing, and delivering images via Cloudflare’s global CDN.
With Cloudflare Images, you can:
- Store and manage images without maintaining your own servers.
- Automatically generate optimized, resized versions.
- Serve images quickly worldwide through Cloudflare’s CDN.
Overview
This gem creates a custom Active Storage service, allowing you to:
- Upload images to Cloudflare Images.
- Generate optimized URLs with automatic transformations.
- Delete images automatically from Cloudflare Images when Active Storage blobs are destroyed.
- Direct upload from browser to Cloudflare Images.
Installation
- Add this line to your application's Gemfile:
gem "unofficial-cloudflare-images"- And then execute:
$ bundle install- Add the following to your encrypted credentials file:
cloudflare_image:
account_id: YOUR_ACCOUNT_ID
token: YOUR_API_TOKEN- Mount the engine in your host
config/routes.rbfile:
mount UnofficialCloudflareImages::Engine => "/unofficial_cloudflare_images"Storage Configuration
Configure Cloudflare Images as an Active Storage service in config/storage.yml:
cloudflare_image:
service: CloudflareImageUsage
Method 1: Client-side upload
Using the unofficial-cloudflare-images-uploader npm package, you can upload images directly to Cloudflare Images from client side.
Here, your browser requests a signed upload URL from Rails, then uploads directly to Cloudflare. Once complete, it notifies Rails to create the Active Storage blob record.
Method 2: Server-side upload
Upload images through your Rails application server.
- Upload an Image:
file = File.open("path/to/image.png")
blob = ActiveStorage::Blob.create_and_upload!(
io: file,
filename: "image.png",
content_type: "image/png",
service_name: "cloudflare_image"
)- Attach to a model:
blob = ActiveStorage::Blob.find(id)
user.avatar.attach(blob)- Get image URL:
blob = ActiveStorage::Blob.find(id)
blob.url- Download an image:
blob = ActiveStorage::Blob.find(id)
blob.download- Deleting an Image
blob = ActiveStorage::Blob.find(id)
blob.destroyHow It Works
This gem implements a custom Active Storage service by extending ActiveStorage::Service.
Key Components:
- CloudflareImageService - The custom Active Storage service class that implements the storage interface.
- CloudflareImageUploaderService - Handles the logic to directly upload images Cloudflare API.
- CloudflareImageDestroyService - Manages deletion of images from Cloudflare.
- CloudflareImagesController - Controller to generated signed upload url.
- DirectUploadsController - Controller to create blob with the uploaded image.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/paulsojan/unofficial-cloudflare-images-gem.
License
The gem is available as open source under the terms of the MIT License.