Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3: tracking overall upload progress for multiple files. #785

Closed
andrey-bahrachev opened this issue Nov 10, 2015 · 11 comments
Closed

S3: tracking overall upload progress for multiple files. #785

andrey-bahrachev opened this issue Nov 10, 2015 · 11 comments
Labels
feature-request A feature should be added or improved.

Comments

@andrey-bahrachev
Copy link

Hey there!
Currently it's possible to keep track of upload progress only on per file basis, but there's no way of calculating the overall upload progress when multiple files are uploaded in parallel. It would be really great to have such ability. Thanks.

@andrey-bahrachev
Copy link
Author

Ah, found a way around using this in the upload progress callback.

s3.upload(params, function (err, data) {
    ...
}).on('httpUploadProgress', function(progress) {
    // Here you can use `this.body` to determine which file this particular
    // event is related to and use that info to calculate overall progress.
});

I hope this will help someone else.
This wasn't obvious though and it still would be nice to have an easier way of doing this.

@chrisradek
Copy link
Contributor

@andrey-bahrachev
We do have a pull request, #679 that would expose the object's key as part of the progress. I had a comment on it, but can look into expediting merging it in.

@andrey-bahrachev
Copy link
Author

@chrisradek Thanks for quick response.
That would be awesome!

@chrisradek
Copy link
Contributor

I've merged in #788. This will be available via npm with the next release of the SDK, otherwise you can install the SDK directly from github.

@andrey-bahrachev
Copy link
Author

Brilliant! Thanks a lot!

chrisradek added a commit that referenced this issue Nov 12, 2015
@rossthedevigner
Copy link

@andrey-bahrachev Do you have an example how you're monitoring overall progress based on this.body info?

@andrey-bahrachev
Copy link
Author

andrey-bahrachev commented Apr 13, 2017

@rossthedevigner Oh, it's been a while, so chances are that things might have changed since then, but here it is:

/**
 * Uploads file(s) directly to AWS S3.
 * 
 * @param form -  jQuery object representing the form.
 * @param {array} files -  Files to be uploaded (derived from HTML5 File API).
 * @param successCallback - Callback function to be called upon successful file(s) upload.
 */
uploadFiles: function(form, files, successCallback) {
    var filesData = [];
    var sizeTotal = 0;
    var loaded = [];
    for (var i = 0; i < files.length; i++) {
        filesData.push({name: files[i].name, size: files[i].size, type: files[i].type});
        sizeTotal += files[i].size;
        loaded[files[i].name] = 0;
    }
    app.checkStorage(form, filesData, function(resp) {
        // Initialize the Amazon Cognito credentials provider.
        AWS.config.region = resp.cognitoRegion;
        AWS.config.sslEnabled = true;
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({IdentityPoolId: resp.cognitoPoolId});
        // Initialize S3 service handler.
        var s3 = new AWS.S3({params: {Bucket: resp.bucket}});
        var uploadBar = form.find('.upload-bar');
        var uploadBarMeter = uploadBar.find('div');
        uploadBar.removeClass('hidden');
        for (var i = 0, filesLen = files.length; i < filesLen; i++) {
            var params = {Key: resp.files[i].key, ContentType: files[i].type, Body: files[i], ACL: 'private'};
            s3.upload(params, function (err, data) {
                if (err) {
                    yii.app.activateForm(form);
                    swal('File upload error');
                    return false;
                }
            }).on('httpUploadProgress', function(e) {
                loaded[this.body.name] = e.loaded;
                var loadedTotal = 0;
                for (var j in loaded) {
                    loadedTotal += loaded[j];
                }
                var progress = Math.round(loadedTotal / sizeTotal * 100);
                uploadBarMeter.css({width: progress + '%'});
                if (loadedTotal === sizeTotal) {
                    successCallback(form, {files: resp.files, form: form.serialize()});
                }
            });
        }
    });
},

@smrgrg
Copy link

smrgrg commented Aug 3, 2018

Thanks a lot!

@srchase srchase added feature-request A feature should be added or improved. and removed enhancement labels Jan 4, 2019
@vedangkoolkarni
Copy link

God bless you for this!

@venu20
Copy link

venu20 commented May 15, 2019

@andrey-bahrachev with the above approach, If i make 20 to 30k calls it would be really slow. Is there any other approach to do bulk upload to S3 ?

@lock
Copy link

lock bot commented Sep 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

7 participants