addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
const path = url.pathname
const bucketUrl = 'https://your-r2-bucket.r2.cloudflarestorage.com' + path
const response = await fetch(bucketUrl)
if (!response.ok) {
return new Response('File not found', { status: 404 })
}
return new Response(response.body, {
headers: { 'content-type': response.headers.get('content-type') }
})
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url)
const path = url.pathname
const bucketUrl = 'https://your-r2-bucket.r2.cloudflarestorage.com' + path
const response = await fetch(bucketUrl)
if (!response.ok) {
return new Response('File not found', { status: 404 })
}
return new Response(response.body, {
headers: { 'content-type': response.headers.get('content-type') }
})
}