Image upload and QR code generation service based on Cloudflare Workers + R2.
- πΌοΈ Image Upload - Supports common formats like JPG, PNG, GIF, WebP
- π± QR Code Generation - Automatically generates QR codes for image links
- π Multi-language Support - Chinese, English, Japanese, Korean
- π¨ Theme Toggle - Light/Dark dual themes
- β‘ Fast Response - Deployed on Cloudflare's global edge network
- π° Low Cost - R2 has no egress fees
- Node.js 18+
- Cloudflare account
- Wrangler CLI
npm install- Log in to Cloudflare Dashboard
- Select your account
- Navigate to R2 or Workers & Pages > R2 in the left menu
- Click the Create bucket button
- Enter bucket name:
qrcode-images - Select location (optional, recommended to choose the region closest to you)
- Click Create bucket to complete
# Log in to Cloudflare (if not already logged in)
npx wrangler login
# Create R2 bucket
npx wrangler r2 bucket create qrcode-imagesImportant: By default, R2 buckets are private. You need to configure access:
Option 1: Access via Workers Proxy (Recommended, used by this project)
- No additional configuration needed, the code already implements proxy access via
/images/:fileNameroute - This approach is more secure and allows access control
Option 2: Configure Public Access (Not recommended for production)
- In Cloudflare Dashboard, go to R2 > your bucket
- Click Settings > Public Access
- Configure custom domain or use Cloudflare's public URL
β οΈ Note: Public access will incur traffic fees- After configuring public access, set the
R2_PUBLIC_URLsecret to use direct R2 links for QR codes:wrangler secret put R2_PUBLIC_URL # Enter the public URL, e.g.: https://xxx.r2.cloudflarestorage.com/qrcode-images # Or custom domain: https://your-custom-domain.com
npm run devVisit http://localhost:8787
npm run deployqrcode/
βββ src/
β βββ index.ts # Worker main file (API + Frontend)
βββ package.json # Dependencies configuration
βββ wrangler.toml # Cloudflare configuration
βββ tsconfig.json # TypeScript configuration
βββ README.md # Project documentation
Upload an image and generate a QR code
Request: multipart/form-data
file: Image file
Response:
{
"success": true,
"imageUrl": "https://your-worker.workers.dev/images/xxx.png",
"qrCode": "data:image/png;base64,xxx",
"fileName": "xxx.png"
}Generate a QR code for a custom URL
Request: application/json
{
"url": "https://example.com",
"size": 300,
"darkColor": "#000000",
"lightColor": "#ffffff"
}Get uploaded image
- Runtime: Cloudflare Workers
- Storage: Cloudflare R2
- Framework: Hono
- QR Code: qrcode
- Language: TypeScript
Configure R2 bucket binding in the wrangler.toml file:
name = "qrcode-generator" # Worker name
main = "src/index.ts" # Entry file
compatibility_date = "2024-11-01" # Compatibility date
# R2 Bucket Configuration
[[r2_buckets]]
binding = "R2_BUCKET" # Binding name used in code
bucket_name = "qrcode-images" # R2 bucket name (must match the bucket name created in Dashboard)Configuration Notes:
binding: Variable name accessed viac.env.R2_BUCKETin Worker codebucket_name: Actual name of the R2 bucket created in Cloudflare Dashboard- You can configure multiple R2 buckets, each bucket requires a separate
[[r2_buckets]]configuration block
In TypeScript code, R2 bucket is accessed through environment bindings:
type Bindings = {
R2_BUCKET: R2Bucket; // Type definition
};
// Upload file to R2
await c.env.R2_BUCKET.put(fileName, arrayBuffer, {
httpMetadata: {
contentType: file.type,
},
});
// Read file from R2
const object = await c.env.R2_BUCKET.get(fileName);Using Local R2 Emulator (Recommended for development):
Wrangler automatically uses the local emulator, no additional configuration needed. Running npm run dev will automatically start the local R2 emulator.
Connecting to Remote R2 Bucket:
If you want to connect to a real R2 bucket during local development, ensure:
- You have logged in via
npx wrangler login - The corresponding bucket has been created in Cloudflare Dashboard
- The bucket name matches the
bucket_nameinwrangler.toml
Pre-deployment Checklist:
- β R2 bucket created in Cloudflare Dashboard
- β
bucket_nameinwrangler.tomlmatches the name in Dashboard - β
Logged in to Cloudflare account via
npx wrangler login - β Worker has permission to access R2 bucket (automatically authorized on first deployment)
Deployment command:
npm run deployIf you need to use different buckets for different environments (development, production):
# Development environment
[env.development]
[[env.development.r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "qrcode-images-dev"
# Production environment
[env.production]
[[env.production.r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "qrcode-images"Deploy to specific environment:
# Deploy to development environment
npx wrangler deploy --env development
# Deploy to production environment
npx wrangler deploy --env productionCurrently supports the following languages:
- π¨π³ Simplified Chinese (zh)
- πΊπΈ English (en)
- π―π΅ Japanese (ja)
- π°π· Korean (ko)
Language settings are automatically saved to localStorage.
- Quickly share images with friends
- Generate product image QR codes for offline promotion
- Event photo sharing
- Any scenario that requires sharing images via QR codes
MIT License