No release in over 3 years
Unofficial gem to upload images to Cloudflare Images
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 7.2.2.2
 Project Readme

Unofficial Cloudflare Images Gem

Gem Version

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

  1. Add this line to your application's Gemfile:
gem "unofficial-cloudflare-images"
  1. And then execute:
$ bundle install
  1. Add the following to your encrypted credentials file:
cloudflare_image:
  account_id: YOUR_ACCOUNT_ID
  token: YOUR_API_TOKEN
  1. Mount the engine in your host config/routes.rb file:
mount UnofficialCloudflareImages::Engine => "/unofficial_cloudflare_images"

Storage Configuration

Configure Cloudflare Images as an Active Storage service in config/storage.yml:

cloudflare_image:
  service: CloudflareImage

Usage

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.destroy

How It Works

This gem implements a custom Active Storage service by extending ActiveStorage::Service.

Key Components:

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.