Skip to content

Commit 11a4e2d

Browse files
committed
10mb max size
1 parent 8c0d1ea commit 11a4e2d

11 files changed

Lines changed: 56 additions & 14 deletions

File tree

dist/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder contains the built output assets for the worker "r2-uploader-example-worker" generated at 2026-03-19T18:04:58.000Z.

dist/index.js.map

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/worker.js

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "r2-uploader-example-worker",
33
"scripts": {
44
"dev": "wrangler dev src/index.ts --port 4897",
5-
"build": "wrangler deploy --dry-run --outfile dist/worker.js src/index.ts",
5+
"build": "wrangler deploy --dry-run --outdir dist && mv dist/index.js dist/worker.js",
66
"deploy": "wrangler deploy --minify src/index.ts"
77
},
88
"dependencies": {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import MpuSupport from './routes/mpu/support'
1313

1414
import checkHeader from "./middleware/checkHeader"
1515

16+
const MAX_UPLOAD_SIZE = 10 * 1024 * 1024
17+
1618
interface Env {
1719
Bindings: {
1820
R2_BUCKET: R2Bucket
1921
UPLOAD_LIMITER: Ratelimit
2022
READ_LIMITER: Ratelimit
23+
UPLOAD_SIZES: KVNamespace
2124
}
2225
}
2326

src/routes/mpu/abort.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default async function (c: Context) {
1515

1616
try {
1717
await multipartUpload.abort();
18+
await c.env.UPLOAD_SIZES.delete(uploadId)
1819
} catch (error: any) {
1920
return new Response(error.message, {status: 400})
2021
}

src/routes/mpu/complete.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default async function (c: Context) {
3232

3333
try {
3434
const object = await multipartUpload.complete(body.parts)
35+
await c.env.UPLOAD_SIZES.delete(uploadId)
3536
return new Response(null, {
3637
headers: {
3738
etag: object.httpEtag

src/routes/mpu/create.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default async function (c: Context) {
88
}
99

1010
const upload = await c.env.R2_BUCKET.createMultipartUpload(key)
11+
12+
await c.env.UPLOAD_SIZES.put(upload.uploadId, '0')
1113

1214
return c.json({
1315
uploadId: upload.uploadId,

src/routes/mpu/parts.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Context} from "hono"
22

3+
const MAX_UPLOAD_SIZE = 10 * 1024 * 1024
4+
35
export default async function (c: Context) {
46
const key = c.req.param('key')
57
const uploadId = c.req.query('uploadId')
@@ -15,6 +17,15 @@ export default async function (c: Context) {
1517
)
1618

1719
const part = await c.req.blob()
20+
21+
const currentSize = parseInt(await c.env.UPLOAD_SIZES.get(uploadId) || '0')
22+
const newSize = currentSize + part.size
23+
24+
if (newSize > MAX_UPLOAD_SIZE) {
25+
return c.text(`Upload exceeds 10MB limit (current: ${currentSize}, part: ${part.size})`, 413)
26+
}
27+
28+
await c.env.UPLOAD_SIZES.put(uploadId, newSize.toString())
1829

1930
const uploaded = await multipartUpload.uploadPart(
2031
parseInt(partNumber),

src/routes/put.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Context} from "hono"
22

3+
const MAX_UPLOAD_SIZE = 10 * 1024 * 1024
4+
35
export default async function (c: Context) {
46
const key = c.req.param('key')
57
const file = await c.req.blob()
@@ -9,6 +11,10 @@ export default async function (c: Context) {
911
return c.text('file name is required', 400)
1012
}
1113

14+
if (file.size > MAX_UPLOAD_SIZE) {
15+
return c.text('File too large. Maximum size is 10MB', 413)
16+
}
17+
1218
await bucket.put(key, file, {
1319
httpMetadata: {
1420
contentType: file.type

0 commit comments

Comments
 (0)