这个脚本是返回网络错误的原因么?

时间:2014-05-13 10:26:59

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

以下代码来自http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-examples.html

我想创建一个简单的表单,将所选文件上传到我的S3存储桶。

然而,它无法正常响应网络错误。有什么问题?

<!DOCTYPE html>
<html>
<head>

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.0-rc.15.min.js"></script>
</head>

<input type="file" id="file-chooser" /> 
<button id="upload-button">Upload to S3</button>
<div id="results"></div>

<script type="text/javascript">
  AWS.config.update({accessKeyId: 'key', secretAccessKey: 'secretkey'});
    AWS.config.region = 'ap-northeast-1';
  var bucket = new AWS.S3({params: {Bucket: 'databackup'}});

  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 ? 'ERROR!' : 'UPLOADED.';
      });
    } else {
      results.innerHTML = 'Nothing to upload.';
    }
  }, false);
</script>

<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您是否更改了以下行的访问信息?

AWS.config.update({accessKeyId:&#39; key&#39;,secretAccessKey:&#39; secretkey&#39;});

您需要更换密钥&#39;和#keykeykey&#39;与您的帐户详细信息。