0

Using Ionic 4.4.0 and aws-sdk 2.157.0. I'm trying to create an S3 bucket from my local web browser, but am running into CORS problems when attempting to run the following code, method createBucketByCompanyKey():

import { Injectable } from '@angular/core';
import * as AWS from 'aws-sdk';

@Injectable()
export class AwsProvider {

  private accessKeyId:string = 'myAccessKey';
  private secretAccessKey:string = 'mySuperSecret';
  private region:string = 'us-east-1';

  constructor() {
    AWS.config.update({accessKeyId: this.accessKeyId, secretAccessKey: this.secretAccessKey, region: this.region});
  }

  createBucketByCompanyKey(companyKey){

    let s3 = new AWS.S3();
    let params = {
      Bucket: companyKey,
      CreateBucketConfiguration: {
        LocationConstraint: this.region
      }
    };
    s3.createBucket(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
  }
}

This gives me the error

Failed to load https://s3.amazonaws.com/-KwzdjmyrHiMBCqHH1ZC: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 403.

Which led me to this post here after several hours of googling. It appears I need to run ionic through a proxy. I've also tried changing my "path" to http://localhost:8100, but stuck I remain.

{
  "name": "MyApp",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {},
  "proxies": [
    {
      "path": "/",
      "proxyUrl": "https://s3.amazonaws.com/"
    }
  ]
}

I've also come across posts telling my to download a Chrome extension that disables CORS, but that didn't work either.

Any ideas on how to setup this proxy to work with AWS' SDK?

Will Lovett
  • 1,122
  • 2
  • 13
  • 29

1 Answers1

0

Forget the proxies. For Mac, enter in the following in the terminal to open a Google Chrome browser with CORS disabled.

open -a Google\ Chrome --args --disable-web-security --user-data-dir

Compliments of this post.

Will Lovett
  • 1,122
  • 2
  • 13
  • 29