Skip to content

Commit

Permalink
feat(server): r2 file retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Sep 25, 2023
1 parent 10220d4 commit 01a3bdf
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/api/cdn/[...path].get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { R2Bucket } from '@cloudflare/workers-types'

export default defineCachedEventHandler(async (event) => {
const bucket: R2Bucket = event.context.cloudflare.env.R2_SSTAA
const file = await bucket.get(event.context.params!.path)
if (!file) {
throw createError({
statusCode: 404,
statusMessage: 'Not found',
})
}

return sendStream(event, file.body as any)
}, {
maxAge: 60,
})

0 comments on commit 01a3bdf

Please # to comment.