简体   繁体   中英

Chunk uploading directly to s3 jquery-file-upload, only last chunk is saved

I am using the Blueimp/jquery-file-upload plugin for uploading files to AWS S3 directly from JS client code. I used multipart upload. Here is setup of fileupload:

multipart: true, maxChunkSize: 10 * 1024 * 1024, autoUpload: true,

When I log responses with this event listener: 'fileuploadchunkdone' I can see that all chunks uploaded as well, but in the bucket only last chunk is saved. Also when I watch my bucket during uploading I can see that the file size is equal chunk size, but after upload complete size of the file equals last chunk size, and file is corrupted.

Am i doing wrong something? Or this is some bug?

Here is my S3 settings:

CORS:

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <ExposeHeader>ETag</ExposeHeader>
    <AllowedHeader>Content-Type</AllowedHeader>
    <AllowedHeader>Content-Range</AllowedHeader>
    <AllowedHeader>Content-Disposition</AllowedHeader>
    <AllowedHeader>x-amz-acl</AllowedHeader>
    <AllowedHeader>x-amz-meta-qqfilename</AllowedHeader>
    <AllowedHeader>x-amz-date</AllowedHeader>
    <AllowedHeader>authorization</AllowedHeader>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Thanks for help!

Update:

Here is js code that i use for this:

$('.upload-video-part').each(function(){
  var that = this;
  var videoId;
  $(this).fileupload({
    sequentialUploads: true,
    multipart: true,
    disableValidation: true,
    maxChunkSize: 10 * 1024 * 1024,
    autoUpload: true,
    add: function (event, data) {
      $(that).find('input[type=file]').hide();
      $(that).find('#progress').show();
      $.ajax({
        url: "/uploaded_videos",
        type: 'POST',
        dataType: 'json',
        data: {doc: {title: data.files[0].name}},
        async: false,
        success: function(retdata) {
          $(that).find('input[name=key]').val(retdata.key);
          $(that).find('input[name=policy]').val(retdata.policy);
          $(that).find('input[name=signature]').val(retdata.signature);
          $(that).parent().append('<div id="video-edit-form-' + retdata.id + '"></div>');
          videoId = retdata.id;
          data.submit();
          $.get({
            url: "/edit_after_upload?id=" + videoId,
            success: function(data) {
              $('#edit_uploaded_video_' + retdata.id).submit(function(e, data){
                e.preventDefault();
                var messageArea = $('#edit_uploaded_video_' + retdata.id).find('.message-area')[0];
                $.ajax({
                  type: 'PUT',
                  url: $('#edit_uploaded_video_' + retdata.id).attr('action'),
                  data: $('#edit_uploaded_video_' + retdata.id).serialize(),
                  success: function(data) {
                    $(messageArea).css('color', 'rgba(0, 255, 0, .5');
                    messageArea.innerHTML = 'Video was successfuly saved!';
                    $(messageArea).css('display', 'inline-block');
                  },
                  error: function(err){
                    $(messageArea).css('color', 'rgba(255, 0, 0, .5');
                    messageArea.innerHTML = err.responseJSON.errors;
                    $(messageArea).css('display', 'inline-block');
                  }
                });
                return false;
              });
            }
          });
        }
      });
    },
    send: function(e, data) {},
    fail: function(e, data) {
      $(that).find('.fileinput-button span')[0].innerHTML = 'Video uploading failed';
      $(that).find('#progress .bar').css('background', 'red');
    },
    done: function (event, data) {
      $(that).find('.fileinput-button span')[0].innerHTML = 'Video successfully uploaded';
      $(that).find('#progress .bar').css('background', 'green');
    },
    progressall: function (e, data) {
      var progress = parseInt(data.loaded / data.total * 100, 10);
      $(that).find('#progress .bar').css( 'width', progress + '%');
    }
  }).on('fileuploadchunksend', function (e, data) {});
});

To upload file in multiple chunks in s3. First Create Multi Part Upload with s3 api.

https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html

From this api you will receive Upload id.

Then use this api to upload parts provide partNumber and upload id in the data.

https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPart.html#API_UploadPart_RequestSyntax

Use etag and partNumber receive from this api to complete partUpload with API_CompleteMultipartUpload

https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM