Store and Deliver images with R2 backend Cloudflare Workers.

Overview

r2-image-worker

Store and Deliver images with Cloudflare R2 backend Cloudflare Workers.

Synopsis

  1. Deploy r2-image-worker to Cloudflare
  2. Make a base64 strings from the image file such as .png, jpg, or gif.
  3. PUT the base64 strings to r2-image-worker.
  4. Image binary will be stored in Cloudflare R2 storage.
  5. r2-image-worker will respond the key of the stored image. abcdef.png
  6. r2-image-worker serve the images on https://r2-image-worker.username.workers.dev/abcdef.png
  7. Images will be cached in Cloudflare KV.
User => Image => base64 => r2-image-worker => R2
User <= Image <= r2-image-worker <= KV <= R2

Prerequisites

  • Cloudflare Account
  • Access privilege for Cloudflare R2 beta
  • Wrangler CLI

Set up

First, git clone

git clone https://github.com/yusukebe/r2-iamge-worker.git
cd r2-image-worker

Create R2 bucket:

wrangler r2 bucket create images

Create KV namespace:

wrangler kv:namespace create "R2_IMAGE_KV"
wrangler kv:namespace create "R2_IMAGE_KV" --preview

Copy wrangler.example.toml to wrangler.toml:

cp wrangler.example.toml wrangler.toml

Edit wrangler.toml.

Variables

Secret variables

Secret variables are:

  • NAME - User name of basic auth
  • PASS - User password of basic auth

To set these, use wrangler secret put command:

wrangler secret put NAME

Publish

To publish to your Cloudflare Workers:

npm run deploy

Endpoints

/upload

Header:

To pass the Basic Auth, add the Base64 string of "user:pass" to Authorization header.

Authorization: Basic ...

Body:

Value of body is Basic64 string of image binary.

{
  "body": "Base64 Text..."
}

Use with Shortcuts

Awesome!!!

SS

Setting shortcuts like this:

Screenshot

Author

Yusuke Wada https://github.com/yusukebe

License

MIT

You might also like...

🪦 Redis Key Value store backed by IPFS

🪦 Redis Key Value store backed by IPFS

🪦 RipDB 🪦 A snappy, decentralized JSON store perfect for fast moving web3 builders. Redis + IPFS = RIP = 😍 Install With a Package Manager (browser

Dec 13, 2022

Backend serverless que simula o sistema de votação do BBB

Backend serverless que simula o sistema de votação do BBB

SSS - Super Serverless Sample Esse é um projeto de exemplo de arquitetura serverless usando como inspiração a votação para eliminação do paredão do BB

Aug 23, 2022

Job queues and scheduled jobs for Node.js, Beanstalkd and/or Iron.io.

Ironium Job queues and scheduled jobs for Node.js backed by Beanstalk/IronMQ/SQS. The Why You've got a workload that runs outside the Web app's reques

Dec 14, 2022

Bree is the best job scheduler for Node.js and JavaScript with cron, dates, ms, later, and human-friendly support.

Bree is the best job scheduler for Node.js and JavaScript with cron, dates, ms, later, and human-friendly support.

The best job scheduler for Node.js and JavaScript with cron, dates, ms, later, and human-friendly support. Works in Node v10+ and browsers, uses workers to spawn sandboxed processes, and supports async/await, retries, throttling, concurrency, and graceful shutdown. Simple, fast, and lightweight. Made for @ForwardEmail and @ladjs.

Dec 30, 2022

Type-safe and Promisified API for Web Worker and Iframe

💛 You can help the author become a full-time open-source maintainer by sponsoring him on GitHub. typed-worker Install npm i typed-worker Usage Create

Dec 31, 2022

Premium Queue package for handling distributed jobs and messages in NodeJS.

Premium Queue package for handling distributed jobs and messages in NodeJS.

The fastest, most reliable, Redis-based queue for Node. Carefully written for rock solid stability and atomicity. Sponsors · Features · UIs · Install

Dec 31, 2022

Redis-backed task queue engine with advanced task control and eventual consistency

idoit Redis-backed task queue engine with advanced task control and eventual consistency. Task grouping, chaining, iterators for huge ranges. Postpone

Dec 15, 2022

A fast, robust and extensible distributed task/job queue for Node.js, powered by Redis.

Conveyor MQ A fast, robust and extensible distributed task/job queue for Node.js, powered by Redis. Introduction Conveyor MQ is a general purpose, dis

Dec 15, 2022

Build and deploy a roadmap voting app for your porject

Build and deploy a roadmap voting app for your porject

Roadmap Voting App You can deploy Roadmap application yourself and get feedback from your users about your roadmap features. See the live example. In

Jan 3, 2023
Comments
  • Add a simple index page for r2-image-worker

    Add a simple index page for r2-image-worker

    Hi yusukebe, Thanks for your awesome work. I think use command line upload file every is inconvenient. So I add a simple index web page for the project.

    • when somebody paste a image in browser, it would show Uploading.
    image
    • when upload finished, it would show the image and url.
    image

    Is it ok to merge?

    Thanks.

    opened by imfht 1
  • update readme

    update readme

    Hi Yusuke, Thanks for your awesome working. Here is a little bug: code is using USER env and readme write NAME . deploy with the wrong env would cause 500 error. I corrected it and add a bash example. Is it ok to merge?

    Thanks!

    opened by imfht 1
  • How to store same base64 on each uplaod ?

    How to store same base64 on each uplaod ?

    Presently I try to upload the same base64 image and it returns the same image name in response. What if multiple users can have the same image And they will try to upload the Same base64 string they will have the same image name, Can't we do that each user will have different image name or name though passing same base64 string or pass same image .

    I know technically it correct to store a single instance for same image , but still is it possible ?

    What if I want to pass the Custom File name to store upload?

    Is there any way to Pass muliptle files in single calls?

    TIA

    opened by marxyes 0
  • Change Secrate Variable to USER insted of NAME

    Change Secrate Variable to USER insted of NAME

    In code you have used secrate variable USER and in Readme You said NAME Either change USER => NAME in Code interface Env { BUCKET: R2Bucket USER: string // change it to NAME: string PASS: string }

    or one should set wrangler secret put USER instead of NAME

    opened by marxyes 0
Owner
Yusuke Wada
Author of @honojs
Yusuke Wada
Cloudflare Worker that will allow you to progressively migrate files from an S3-compatible object store to Cloudflare R2.

A Cloudflare Worker for Progressive S3 to R2 Blog Post: https://kian.org.uk/progressive-s3-to-cloudflare-r2-migration-using-workers/ This is a Cloudfl

Kian 29 Dec 30, 2022
slash-create with Cloudflare Workers template

/create with Cloudflare Workers A slash-create template, using Cloudflare Workers. Getting Started Cloning the repo You can either use degit to locall

Snazzah 13 Jan 3, 2023
Airtable + Cloudflare Workers URL Shortener 🌤

tableflare Airtable + Cloudflare Workers URL Shortener ?? Quick Start Airtable Generate your Airtable API key from your account dashboard: https://air

Griko Nibras 10 Oct 16, 2022
Build your Cloudflare Workers with esbuild.

build-worker Bundle your Cloudflare Worker with esbuild instead of webpack. (It's ridiculously faster!) Wrangler v1 uses webpack. Wrangler v2 is using

Rom 7 Oct 24, 2022
Making service workers easy so that your app is fast and reliable, even offline.

tulo.js Making service workers easy to use so that your app can be fast and reliable, even offline. Welcome to tulo.js, a service worker library that

OSLabs Beta 37 Nov 16, 2022
Example repo for getting NextJS, Rust via wasm-pack, and web workers all playing nicely together.

Example of integrating WASM & web workers with a Typescript NextJS project. Running yarn yarn dev Open localhost:3000 Layout Rust code is in ./rust, g

Studio Lagier 63 Dec 23, 2022
Easily redirect one entire domain to another with a serverless Cloudflare Worker.

Domain Redirecting with Cloudflare Workers Easily redirect one entire domain to another with a serverless Cloudflare Worker. All paths and other data

Erisa A 19 Dec 11, 2022
Send emails using Cloudflare Worker, for free.

Email API for Proselog. Not intended for use outside of Proselog, but it should work with any worker, without any configuration. import { sendEmail }

Proselog 65 Nov 7, 2022
Cloudflare Worker to make a R2 Bucket public!

r2-public-worker A Cloudflare Worker to make your R2 bucket public! Minimum Requirements Cloudflare Account wrangler >= 2.0.2 Note: Ensure you are usi

Cole Mackenzie 20 Sep 19, 2022
Using Cloudflare worker to generate host list from firebog to keep updated.

AdGuardCloudflareHostGenerator Use a cloudflare worker to generate a up to date list from FireBog's ticked list found at https://v.firebog.net/hosts/l

Jake Steele 14 Nov 30, 2022