A simple file-sharing service built with Go and Cloudflare R2. Upload files and get shareable download links.
- Upload files through a web interface
- Store files in Cloudflare R2 cloud storage
- Generate unique download links for each file
- Download links expire after 15 minutes for security
- Go 1.25 or higher
- Cloudflare R2 account (free tier available)
git clone https://github.com/heydeepakch/file-sharing-service.git
cd file-sharing-service
go mod download- Go to https://dash.cloudflare.com/
- Navigate to R2
- Click "Create Bucket" and name it (e.g.,
my-file-sharing) - Go to "Manage R2 API Tokens"
- Create a new API token with Edit permissions
- Save these three values:
- Access Key ID
- Secret Access Key
- Endpoint URL (format:
https://[account-id].r2.cloudflarestorage.com)
Create a .env file in the project root:
R2_ACCESS_KEY=your_access_key_id
R2_SECRET_KEY=your_secret_access_key
R2_ENDPOINT=https://your-account-id.r2.cloudflarestorage.com
R2_BUCKET_NAME=my-file-sharing
PORT=8080
BASE_URL=http://localhost:8080Replace the placeholder values with your actual R2 credentials.
go run .The server will start on http://localhost:8080
Open your browser and go to http://localhost:8080
- PDF (.pdf)
- Images (.jpg, .jpeg, .png, .gif)
- Archives (.zip)
Maximum file size: 100 MB
Upload Process:
- User uploads a file via web form
- File is validated for type and size
- File is uploaded to Cloudflare R2
- Metadata is saved to local database (db.json)
- User receives a download link
Download Process:
- User clicks download link
- Server looks up file in database
- Server generates a temporary R2 URL (valid for 15 minutes)
- User is redirected to download directly from R2
file-sharing-service/
├── main.go # Main application and HTTP handlers
├── storage.go # Database functions (JSON file storage)
├── cloud.go # R2 connection configuration
├── static/
│ └── index.html # Upload form
├── .env # Environment variables (create this)
├── db.json # File metadata (auto-generated)
└── go.mod # Go dependencies
| Variable | Description |
|---|---|
| R2_ACCESS_KEY | Your R2 access key ID |
| R2_SECRET_KEY | Your R2 secret access key |
| R2_ENDPOINT | Your R2 endpoint URL |
| R2_BUCKET_NAME | Name of your R2 bucket |
| PORT | Server port (default: 8080) |
| BASE_URL | Base URL for links (default: http://localhost:8080) |
GET /
- Returns upload form
POST /upload
- Upload a file
- Form field:
file - Returns: Download link
GET /download/{id}
- Download a file
- Redirects to temporary R2 URL