Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 JavaScript SDK - NetworkingError: Network Failure

I am trying to use the examples provided by AWS in their Examples in the Browser webpage, and I keep receiving the NetworkingError: Network Failure error. Here is what I am using:

<html>
<head>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.0-rc11.min.js"></script>
<script type="text/javascript">
  // See the Configuring section to configure credentials in the SDK

  AWS.config.update({accessKeyId: '####', secretAccessKey: '####', region: 'us-east-1'});
  // Configure your region
//  AWS.config.region = '';
</script>
</head>
<body>
<input type="file" id="file-chooser" />
<button id="upload-button">Upload to S3</button>
<div id="results"></div>

<script type="text/javascript">
  var bucket = new AWS.S3({params: {Bucket: 'BUCKETNAMEGOESHERE'}});

  var fileChooser = document.getElementById('file-chooser');
  var button = document.getElementById('upload-button');
  var results = document.getElementById('results');
  button.addEventListener('click', function() {
    var file = fileChooser.files[0];
    if (file) {
      results.innerHTML = '';

      var params = {Key: file.name, ContentType: file.type, Body: file};
      bucket.putObject(params, function (err, data) {
        results.innerHTML = err ? err : 'UPLOADED.';
      });
    } else {
      results.innerHTML = 'Nothing to upload.';
    }
  }, false);
</script>

<div id="status"></div>
<ul id="objects"></ul>

<script type="text/javascript">
  var bucket = new AWS.S3({params: {Bucket: 'BUCKETNAMEGOESHERE'}});
  bucket.listObjects(function (err, data) {
    if (err) {
      document.getElementById('status').innerHTML =
        'Could not load objects from S3';
    } else {
      document.getElementById('status').innerHTML =
        'Could not load objects from S3';
    } else {
      document.getElementById('status').innerHTML =
        'Loaded ' + data.Contents.length + ' items from S3';
      for (var i = 0; i < data.Contents.length; i++) {
        document.getElementById('objects').innerHTML +=
          '<li>' + data.Contents[i].Key + '</li>';
      }
    }
  });
</script>
</body>
</html>

My bucket is in the US Standard region, and I am trouble figuring out whether it uses us-west-2 or us-east-1. When I go to my bucket's console, the URL is https://console.aws.amazon.com/s3/home?region=us-west-2 yet someone mentioned on here that US Standard is us-east-1.

like image 458
alexpja Avatar asked Feb 13 '23 11:02

alexpja


2 Answers

This error occurs because you don't set the CORS configuration.

  1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. Select the name of the bucket you are using from the bucket list.

  3. Next, Choose "Permission" tab.

  4. Then in an editor titled "Cross-origin resource sharing (CORS)", you need to make sure the S3 bucket has following CORS configuration:


[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "HEAD",
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "ETag"
        ]
    }
]
like image 132
MsiLucifer Avatar answered Feb 16 '23 04:02

MsiLucifer


As of the writing of this answer, according to the Amazon Web Services General Reference, US Standard uses us-east-1.

like image 43
Wesley Avatar answered Feb 16 '23 04:02

Wesley