0

I am trying to replicate the result from this guide: https://developers.cloudflare.com/r2/data-access/s3-api/presigned-urls/ However, with the exact same code, the signedUrl variable return empty object({}). If anyone has any insight would be much appreciated! According to my understanding and as written in the docs, I don't have to pass any accessKeyId or secretAccessKey. However, the s3 url that I got from my cloudflare dashboard is : https://<ACCOUNT ID>.r2.cloudflarestorage.com/<DROPBOX BUCKET>, which is different from the docs.

const r2 = new AwsClient({
  accessKeyId: "",
  secretAccessKey: "",
  service: "s3",
  region: "auto",
});

app.get("/getUploadUrl", async (c) => {
  const timeOut = +c.req.queries("timeOut") ?? 60 * 5;
  const fileName = c.req.queries("fileName") ?? crypto.randomUUID();
  const bucketName = c.req.header("bucket");
  const bucketUrl = bucketNameToUrl[bucketName];
  console.log("fileName", fileName);
  const signedUrl = await r2.sign(
    new Request(`${bucketUrl}/${fileName}`, {
      method: "PUT",
    }),
    {
      aws: { signQuery: true },
      headers: {
        "X-Amz-Expires": timeOut.toString(),
      },
    }
  );
  return c.json(signedUrl);
});
harpy
  • 1
  • 1
  • Please post text, not images of text. And you absolutely will need an access key and secret key, the signing algorithm requires them both to create the signature. – Anon Coward Jan 16 '23 at 01:30
  • According to the Cloudflare doc, I don't think those are automatically fill in. This code here will be run on the Cloudflare worker environment. – harpy Jan 16 '23 at 02:04

1 Answers1

1

You can also use the URL format: https://<bucket_name>.<account_id>.r2.cloudflarestorage.com both the slash at the end and this version should work.

The Access Key and Secret Access Key need to be generated with the button on the right of the R2 console labelled "R2 API Keys". There's also a Discord server where you can get help and learn a lot about R2, and Cloudflare in general

Alejandra
  • 11
  • 2