A simple file sharing tool running on Cloudflare Workers, supporting R2 and D1 dual storage solutions.
- 🔐 Password protection, supports cookie-based persistent login (30 days)
- 💾 Dual storage solution: R2 storage bucket + D1 database
- 📦 Automatic storage selection: files larger than 25MB automatically use R2
- 🔗 Simple sharing links
- 🎨 Minimalist black and white interface design
- 🚀 Cloudflare Workers driven, global high-speed access
Login process:
User access → Check cookies → No cookies → Display login page → Verify password → Set cookies → Enter home page
↑
Have cookies → Verify cookies → Enter home page
Upload process:
Select file → Check file size → >25MB → Use R2 storage
→ ≤25MB → Select storage method → R2 or D1
↓
Generate unique ID → Store file → Return the sharing link
Download process:
Access the sharing link → Parse the file ID → Determine storage location → Retrieve file → Return file content
- Node.js (16.x or higher)
- Cloudflare account
- Wrangler CLI
-
Clone the repository:
git clone https://github.com/joyance-professional/cf-files-sharing cd cf-files-sharing
-
Install dependencies:
npm install
-
Install wrangler:
npm install -g wrangler
-
Log in to Cloudflare:
wrangler login
-
Create the R2 bucket:
wrangler r2 bucket create file-share
-
Create the D1 database:
wrangler d1 create file-share
-
Update the database ID in the
wrangler.toml
file:[[d1_databases]] binding = "DB" database_name = "file-share" database_id = "your-database-id" # obtained from the previous step
Run the database migration:
wrangler d1 execute file-share --file=./migrations/init.sql --remote
Note
Ensure that you replace "your-database-id"
in the wrangler.toml
file with the actual database ID obtained from the previous steps.
Also, remember to include the --remote
flag when running database migrations to apply them to the remote D1 database.
-
Set the authentication password:
wrangler secret put AUTH_PASSWORD
When prompted, enter the password you want to set.
Deploy to Cloudflare Workers:
wrangler deploy
- Access your Workers domain.
- Enter the set
AUTH_PASSWORD
to log in. - The login status will remain for 30 days.
- After logging in, select the file to upload.
- For files less than 25MB, you can choose the storage method (R2 or D1).
- Files larger than 25MB will automatically use R2 storage.
- Get the sharing link after the upload is complete.
- Share link format:
https://your-worker.workers.dev/file/[FILE_ID]
- Anyone can download the file directly through the link.
- The link is permanently valid.
- R2 Storage: Suitable for large files, no size limit.
- D1 Storage: Suitable for small files (<25MB), stored in SQLite database.
CREATE TABLE files (
id TEXT PRIMARY KEY,
filename TEXT NOT NULL,
size INTEGER NOT NULL,
storage_type TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
content BLOB NULL
);
- Password-protected admin interface
- HttpOnly cookies
- Secure file ID generation mechanism
Variable Name | Description | Required |
---|---|---|
AUTH_PASSWORD | Admin interface login password | Yes |
name = "file-share-worker"
main = "src/index.js"
[[r2_buckets]]
binding = "FILE_BUCKET"
bucket_name = "file-share"
[[d1_databases]]
binding = "DB"
database_name = "file-share"
database_id = "your-database-id"
-
After cloning the repository, run:
wrangler dev
-
Visit
http://localhost:8787
for testing.
cf-files-sharing/
├── src/
│ ├── index.js # Main entry file
│ ├── auth.js # Authentication logic
│ ├── storage/
│ │ ├── r2.js # R2 storage handling
│ │ ├── d1.js # D1 storage handling
│ │ └── manager.js # Storage manager
│ ├── utils/
│ │ ├── response.js # Response utilities
│ │ └── id.js # File ID generator
│ └── html/
│ └── templates.js # HTML templates
├── wrangler.toml # Cloudflare configuration
└── migrations/ # D1 database migrations
└── init.sql
- Fork this repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature
). - Commit your changes (
git commit -m 'Add some AmazingFeature'
). - Push to the branch (
git push origin feature/AmazingFeature
). - Open a Pull Request.
- Cloudflare Workers Platform
- Claude-3.5-Sonnet AI
- Chat-GPT-o1-preview | Chat History
If you find any issues or have suggestions for improvements, please create an issue.