Service Upload with S3 SDK and Cloudflare R2
- CURL example
curl --location 'http://localhost:4100/api/upload' \
--header 'x-api-key: [secret key]' \
--form 'file=@"/path/to/file"'
- Javascript Fetch example
const formData = new FormData()
formData.append('file', fileInput.files[0])
fetch('http://localhost:4100/api/upload', {
method: 'POST',
headers: {
'x-api-key': 'your-secret-key-here',
},
body: formData,
})
.then((res) => res.json())
.then((data) => console.log('Success:', data))
.catch((err) => console.error('Upload error:', err))
- Response
201
{
"data": {
"url": "https://cloudflare.com/file_name.jpeg",
"name": "file_name.jpeg",
"size": 12345
},
"message": "Upload successful"
}
- Response
400
{
"data": null,
"message": "No file uploaded!"
}
- Response
401
{
"error": "Unauthorized"
}
- Response
500
{
"error": "Upload error"
}
- Clone this repository
- Copy
.env.example
to.env
and adjust according to the Cloudflare R2 credentials you have
- Express.js + Typescript
- Sharp (Compress image)
- Aws SDK
- Swagger Docs (here)