AWS开发工具包JavaScript:如何显示AWS.S3.putObject的上传进度?

时间:2014-05-23 17:32:15

标签: javascript html5 file-upload amazon-web-services amazon-s3

我正在开发一个JavaScript客户端,直接将文件上传到Amazon S3。

<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: 'myBucket'}});

  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>

亚马逊文档中的示例运行正常,但它没有提供有关上传进度的任何反馈。

有什么想法吗?

由于

3 个答案:

答案 0 :(得分:12)

更改我的Google查询我发现了我需要的内容:

https://github.com/aws/aws-sdk-js/commit/084f676927b9cd3da337bd6d0d230680c138d73b

文档示例应该有它......

答案 1 :(得分:5)

为什么不使用ManagedUpload函数而不是使用s3.PutObject函数。

它是专门为允许您加入httpUploadProgress事件而设计的,该事件应该可以很容易地实现进度条的更新。

答案 2 :(得分:1)

我已经对文件上传进度进行了一些自定义。在node,angular和javascript中使用相同的逻辑。

这是存储库链接:

https://github.com/aviboy2006/aws-s3-file-upload-progress

使用此小提琴进行测试:https://jsfiddle.net/Lhrem97n/

注意:更新访问密钥,存储桶名称和秘密密钥。

 var bucket = new AWS.S3({
   accessKeyId: "",
   secretAccessKey: "",
   region: 'us-east-1'
 });

 uploadfile = function(fileName, file, folderName) {
   const params = {
     Bucket: "fileuploadprocess",
     Key: folderName + fileName,
     Body: file,
     ContentType: file.type
   };
   return this.bucket.upload(params, function(err, data) {

     if (err) {
       console.log('There was an error uploading your file: ', err);
       return false;
     }
     console.log('Successfully uploaded file.', data);
     return true;
   });
 }

 uploadSampleFile = function() {
   var progressDiv = document.getElementById("myProgress");
   progressDiv.style.display="block";
   var progressBar = document.getElementById("myBar");
   file = document.getElementById("myFile").files[0];
   folderName = "Document/";
   uniqueFileName = 'SampleFile'; 
   let fileUpload = {
     id: "",
     name: file.name,
     nameUpload: uniqueFileName,
     size: file.size,
     type: "",
     timeReference: 'Unknown',
     progressStatus: 0,
     displayName: file.name,
     status: 'Uploading..',
   }
   uploadfile(uniqueFileName, file, folderName)
     .on('httpUploadProgress', function(progress) {
       let progressPercentage = Math.round(progress.loaded / progress.total * 100);
       console.log(progressPercentage);
       progressBar.style.width = progressPercentage + "%";
       if (progressPercentage < 100) {
         fileUpload.progressStatus = progressPercentage;

       } else if (progressPercentage == 100) {
         fileUpload.progressStatus = progressPercentage;

         fileUpload.status = "Uploaded";
       }
     })
 }