AWS S3 JavaScript SDK - NetworkingError:网络故障

时间:2014-03-18 22:51:44

标签: javascript amazon-web-services amazon-s3

我正在尝试使用AWS在浏览器网页的示例中提供的示例,并且我一直收到NetworkingError: Network Failure错误。这是我正在使用的:

<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>

我的广告位于美国标准区域,我很难确定它是使用us-west-2还是us-east-1。当我进入我的水桶控制台时,网址为https://console.aws.amazon.com/s3/home?region=us-west-2,但此处有人提到美国标准为us-east-1

3 个答案:

答案 0 :(得分:2)

截至撰写此答案时,根据Amazon Web Services General Reference,美国标准使用us-east-1

答案 1 :(得分:2)

这已经很晚了,但是如果其他人发现这一点你需要确保你的控制台中的CORS文件有足够的权限。

答案 2 :(得分:0)

出现此错误是因为您没有设置 CORS 配置。

  1. https://console.aws.amazon.com/s3/ 打开 Amazon S3 控制台。

  2. 从存储桶列表中选择您正在使用的存储桶的名称。

  3. 接下来,选择“权限”选项卡。

  4. 然后在标题为“跨域资源共享 (CORS)”的编辑器中,您需要确保 S3 存储桶具有以下 CORS 配置:


[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "HEAD",
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "ETag"
        ]
    }
]