R2 signed url creation from Worker

I am attempting to create a signed url on a worker for R2 using the aws4fetch package, but the signed value being returned is an empty object. async function createSignedUploadUrl(requestPath: string) { const r2 = new AwsClient({ accessKeyId: ACCESS_KEY_ID, secretAccessKey: SECRET_ACCESS_KEY, }); const url = new URL( https://${env.BUCKET_NAME}.${env.ACCOUNT_ID}.r2.cloudflarestorage.com ); url.pathname = requestPath; console.log(url: ${url}); const signed = await r2.sign( new Request(url, { method: "PUT", }), { aws: { signQuery: true }, } ); return signed; } url value is "https://BUCKET_NAME.ACCOUNT_ID.r2.cloudflarestorage.com/icon.png" signed value is {}
2 Replies
kian
kian12mo ago
you’re returning a Request object that won’t log or stringify just return signed.url
garrettAG
garrettAG12mo ago
Thanks Is there a way I can set the file type to be something other than the default of multipart/form-data? If I leave content-type off the sign url creation and upload the file gets uploaded but the multipart/form-data prevents the file from opening when downloaded. I have tried setting headers with allHeaders: true, but then the OPTIONS preflight check on the upload returns a 403, strict-origin-when-cross-origin? const signed = await r2.sign( new Request(url, { method: "PUT", headers: { 'Content-Type': fileType, 'Content-Length': fileSize, } }), { aws: { signQuery: true, allHeaders: true }, } ); The fix for future readers was adding this to the CORS policy "AllowedHeaders": [ "*" ]