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
4 Replies
Walshy
Walshy2mo 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 });
}
}
mau
mauOP5w ago
Do you think it would cost on workers side? Assets in the public directory are delivered using the assets binding in new Open Next.js projects. Here’s the link for reference: https://opennext.js.org/cloudflare/get-started. The issue is that I have to store all my assets in Git, and during deployment, they are automatically delivered via the assets binding. What I want is to achieve the same functionality but with my own R2 bucket. Does that make sense?
mau
mauOP5w ago
Cloudflare Docs
Configuration and binding | Cloudflare Workers docs
Details on how to configure Workers static assets and its binding.
mau
mauOP4w ago
any help? @Cloudflare (Server Owner)
Want results from more Discord servers?
Add your server