how to deliver static assets from another R2 bucket directly like open next?

Hi, I’m integrating the H5P standalone library (https://github.com/tunapanda/h5p-standalone) with our Next.js app. We need to deliver H5P content type libraries and content files. The libraries can be in a shared directory, and the content follows the H5P folder structure. Since Cloudflare is now integrating R2 for static assets with the OpenNext project, how can we deliver these assets from another bucket while maintaining the required folder structure?
GitHub
GitHub - tunapanda/h5p-standalone: Display H5P content without the ...
Display H5P content without the need for an H5P server - tunapanda/h5p-standalone
1 Reply
Walshy
Walshy22h ago
You'll want your Worker to have an R2 binding with your intended bucket and then fetch Not sure what needs to happen to the path but some simple code:
export default {
fetch(req, env) {
const { pathname } = new URL(req.url);

const value = await env.R2.get(pathname);
if (!value) {
return new Response('Not Found', { status: 404 });
}
const headers = new Headers();
value.writeHttpMetadata(headers);

return new Response(value.body, { headers });
}
}
export default {
fetch(req, env) {
const { pathname } = new URL(req.url);

const value = await env.R2.get(pathname);
if (!value) {
return new Response('Not Found', { status: 404 });
}
const headers = new Headers();
value.writeHttpMetadata(headers);

return new Response(value.body, { headers });
}
}
Want results from more Discord servers?
Add your server