<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ehtesham Siddiqui</title>
    <description>The latest articles on DEV Community by Ehtesham Siddiqui (@ehteshamdev).</description>
    <link>https://dev.to/ehteshamdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F174279%2F5382722b-8f79-4fe7-92bc-b410bf81c360.jpeg</url>
      <title>DEV Community: Ehtesham Siddiqui</title>
      <link>https://dev.to/ehteshamdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ehteshamdev"/>
    <language>en</language>
    <item>
      <title>How To Fix - CORS Error while uploading files on Cloudflare R2 using presigned URLs.</title>
      <dc:creator>Ehtesham Siddiqui</dc:creator>
      <pubDate>Mon, 14 Nov 2022 13:55:50 +0000</pubDate>
      <link>https://dev.to/ehteshamdev/how-to-fix-cors-error-while-uploading-files-on-cloudflare-r2-using-presigned-urls-21dm</link>
      <guid>https://dev.to/ehteshamdev/how-to-fix-cors-error-while-uploading-files-on-cloudflare-r2-using-presigned-urls-21dm</guid>
      <description>&lt;p&gt;This post was originally posted &lt;a href="https://ehtesham.dev/blog/how-to-fix-cors-error-while-uploading-files-on-cloudflare-r2-using-presigned-urls"&gt;here&lt;/a&gt; at my blog.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://www.cloudflare.com/en-gb/products/r2/"&gt;Cloudflare R2&lt;/a&gt; Storage allows developers to store large amounts of unstructured data without the costly egress bandwidth fees associated with typical cloud storage services.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While using R2 to upload files with presigned URLs, I started getting the following CORS issue in my browser console:&lt;br&gt;
&lt;em&gt;"Access to fetch at '' from origin &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Here is how I solved it using aws-sdk for js V3:
&lt;/h2&gt;

&lt;p&gt;Create an empty node project and install aws-sdk for js v3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir r2-cors &amp;amp;&amp;amp; cd r2-cors
pnpm init
pnpm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
touch cors.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;type: "module"&lt;/code&gt; into your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Next, copy and paste this snippet into cors.js and replace the credentials from your Cloudflare R2 dashboard. Refer &lt;a href="https://developers.cloudflare.com/r2/data-access/s3-api/tokens"&gt;here&lt;/a&gt; to create the tokens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PutBucketCorsCommand, S3Client } from "@aws-sdk/client-s3";

const s3Client = new S3Client({
  region: "auto",
  endpoint: `https://${CF-ACCOUNT-ID}.r2.cloudflarestorage.com`, //TODO: replace
  credentials: {
    accessKeyId: `${ACCESS-KEY}`, //TODO: replace
    secretAccessKey: `${ACCESS-SECRET}`, //TODO: replace
  },
});

async function main() {
  const response = await s3Client.send(
    new PutBucketCorsCommand({
      Bucket: "your-bucket-name", //TODO: replace
      CORSConfiguration: {
        CORSRules: new Array({
          AllowedHeaders: ["content-type"], //this is important, do not use "*"
          AllowedMethods: ["GET", "PUT", "HEAD"],
          AllowedOrigins: ["*"],
          ExposeHeaders: [],
          MaxAgeSeconds: 3000,
        }),
      },
    })
  );
  console.dir(repsonse)
}

main();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the script with node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node cors.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything works well, you should see the following logged in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  '$metadata': {
    httpStatusCode: 200,
    requestId: undefined,
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it, your CORS issue should be resolved now.&lt;/p&gt;

&lt;p&gt;If you have any questions or just want to connect, follow me on &lt;a href="https://twitter.com/ehteshamdev"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>node</category>
      <category>cors</category>
      <category>cloudflare</category>
      <category>r2</category>
    </item>
  </channel>
</rss>
