CodeTorso
Explore posts from serversCDCloudflare Developers
•Created by CodeTorso on 6/14/2024 in #general-help
Creating presigned urls for R2 with workers, unauthorised error
So I have created this simple example to show you how to code works, the image shows the right my api has, the api is more complex but even this simple example is not working
const url12 = "https://53.fs1.hubspotusercontent-na1.net/hub/53/hubfs/image8-2.jpg?width=595&height=400&name=image8-2.jpg"
// I get the image
const res = await fetch(
${url12})
const image = await res.blob();
// I create a client
const r2 = new AwsClient({
accessKeyId: process.env.R2_ACCESS_KEY_ID,
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY,
});
const bucketName = process.env.R2_BUCKET_NAME;
const accountId =process.env.R2_ACCOUNT_ID;
const url = new URL(
https://${bucketName}.${accountId}.r2.cloudflarestorage.com`
);
url.searchParams.set("X-Amz-Expires", "3600");
url.pathname = "google";
// I get a put request url to store the image
const signed = await r2.sign(
new Request(url, {
method: "PUT",
}),
{
aws: { signQuery: true },
}
);
// I make the request to put the image
await fetch(signed.url, {
method: 'PUT',
body: image,
});
// I create the get request url to return
const signeduser = await r2.sign(
new Request(url, {
method: "GET",
}),
{
aws: { signQuery: true },
}
);
// I return the get
return new Response(signeduser.url) `1 replies