Upload file to R2

Hey guys im using Cloudflare Workers to upload a pdf to R2. im not getting any errors at all. i have the binding setup properly. nothing pops up in my R2 bucket. so weird. any input?
/* 1. Get bytes */
const pdfBytes = await pdfRes.arrayBuffer();

/* 2. Persist to R2 */
const key = `${userId}/${clientId}-${Date.now()}.pdf`;
const putRes = await env.REPORTS.put(key, pdfBytes, {
httpMetadata: { contentType: 'application/pdf' }
}); // put() default is private. ✔
console.log('stored to R2 key →', key, 'etag →', putRes?.etag);

/* 3. Return a link the front-end can hit */
return Response.json({ ok: true, url: `/reports/${key}` });
/* 1. Get bytes */
const pdfBytes = await pdfRes.arrayBuffer();

/* 2. Persist to R2 */
const key = `${userId}/${clientId}-${Date.now()}.pdf`;
const putRes = await env.REPORTS.put(key, pdfBytes, {
httpMetadata: { contentType: 'application/pdf' }
}); // put() default is private. ✔
console.log('stored to R2 key →', key, 'etag →', putRes?.etag);

/* 3. Return a link the front-end can hit */
return Response.json({ ok: true, url: `/reports/${key}` });
1 Reply
Josh
Josh5mo ago
Are you running this locally? You need to use the —remote flag for it to show up online.

Did you find this page helpful?