// get two streams for the body (one for R2, one for responding)
const tee = response.body?.tee()
// if the response status isn't 200, respond with the status code given by the upstream
if (response.status !== 200) {
console.log(`Got HTTP ${response.status} from upstream: ${url}`)
return new Response(null, { status: response.status })
}
// attempt to put the upstream data into R2
try {
await R2_BUCKET.put(key, tee[0])
console.log(`Successfully put object ${key} into R2`)
} catch (err) {
console.log(`Error while putting ${key} into R2:`, err)
}
// return the response from upstream
return new Response(tee[1])
// get two streams for the body (one for R2, one for responding)
const tee = response.body?.tee()
// if the response status isn't 200, respond with the status code given by the upstream
if (response.status !== 200) {
console.log(`Got HTTP ${response.status} from upstream: ${url}`)
return new Response(null, { status: response.status })
}
// attempt to put the upstream data into R2
try {
await R2_BUCKET.put(key, tee[0])
console.log(`Successfully put object ${key} into R2`)
} catch (err) {
console.log(`Error while putting ${key} into R2:`, err)
}
// return the response from upstream
return new Response(tee[1])