Skip to content

Instantly share code, notes, and snippets.

@mikeesto
Last active October 4, 2022 17:45
Show Gist options
  • Select an option

  • Save mikeesto/a702290925d90ee32b550a8981a657d2 to your computer and use it in GitHub Desktop.

Select an option

Save mikeesto/a702290925d90ee32b550a8981a657d2 to your computer and use it in GitHub Desktop.

Revisions

  1. mikeesto revised this gist Oct 3, 2022. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions cors.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    import aws from "aws-sdk";

    const s3 = new aws.S3({
    endpoint: `https://${process.env.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
    accessKeyId: process.env.CLOUDFLARE_ACCESS_KEY,
    secretAccessKey: process.env.CLOUDFLARE_SECRET_ACCESS_KEY,
    signatureVersion: "v4",
    });

    const config = {
    AllowedHeaders: ["content-type"], // This seems to be necessary! Not including it gives errors. Putting it as * still gives CORS errors
    AllowedMethods: ["PUT"],
  2. mikeesto created this gist Oct 3, 2022.
    18 changes: 18 additions & 0 deletions cors.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    const config = {
    AllowedHeaders: ["content-type"], // This seems to be necessary! Not including it gives errors. Putting it as * still gives CORS errors
    AllowedMethods: ["PUT"],
    AllowedOrigins: ["*"],
    };

    const bucketParams = {
    Bucket: process.env.CLOUDFLARE_BUCKET_NAME,
    CORSConfiguration: { CORSRules: new Array(config) },
    };

    s3.putBucketCors(bucketParams, function (err, data) {
    if (err) {
    console.log("Error", err);
    } else if (data) {
    console.log("Success", JSON.stringify(data.CORSRules));
    }
    });