Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// 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 = ''; 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); 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 += ' } } }); |
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
.
Answer:
This error occurs because you don’t set the CORS configuration.
- Open the Amazon S3 console at https://console.aws.amazon.com/s3/.
- Select the name of the bucket you are using from the bucket list.
- Next, Choose “Permission” tab.
- Then in an editor titled “Cross-origin resource sharing (CORS)”, you need to make sure the S3 bucket has following CORS configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[ { "AllowedHeaders": [ "*" ], "AllowedMethods": [ "HEAD", "GET", "PUT", "POST", "DELETE" ], "AllowedOrigins": [ "*" ], "ExposeHeaders": [ "ETag" ] } ] |