1

Im pretty new to AWS and MeteorJ and I'm having issue downloading large files (100mb+). I would like the user to click the download button and the file start downloading right away. I might be wrong but the code looks like is downloading the file into memory and then sending it to the client-side.

EDIT: The question is a bit different. I'm not calling the server function via http. I added the call to the server.

Here is the call to the server:

function getFile(data) {
    const def = $q.defer();

    Meteor.call('downloadFileFromS3', data, (err, res) => {
        if (err) {
            return def.reject(err)
        }

        return def.resolve(res)
    });

    return def.promise;
};

Here is the meteorjs code:

async downloadFileFromS3(params) {
    try {
        const criteria = {
            Bucket: params.bucket,
            Key: `${params.key}/${params.filename}`
        };

        const data = await s3.getObject(criteria).promise();

        return {
            data: data.Body,
            contentType: data.ContentType,
            filename: params.filename
        };
    } catch (ex) {
        throw new Meteor.Error('500', ex.message);
    }
}

Here is what I'm doing in the client side:

function _download (data, filename) {
    const blob = new Blob([data.data], {type: data.contentType});

    const url = window.URL.createObjectURL(data);
    const el = document.createElement('a');

    el.style = 'display: none';
    el.href = url;
    el.download = filename;

    document.body.appendChild(el);

    el.click();

    window.URL.revokeObjectURL(url);
}
chr.solr
  • 576
  • 1
  • 7
  • 19
  • How do I do that in my scenario. I'm not using a hiring the end point via Ajax. – chr.solr Jul 23 '18 at 23:54
  • Meant *hitting not hiring – chr.solr Jul 23 '18 at 23:55
  • Sorry, I'm on my phone. I'm not hitting the API via Ajax. – chr.solr Jul 23 '18 at 23:59
  • @georgeawg Can you reopen the question? or can you explain how can i accomplish it in meteorjs? – chr.solr Jul 24 '18 at 18:51
  • Possible duplicate of [Download large size files with angular file saver](https://stackoverflow.com/a/42549677/5535245). If amazon-s3 API has an option to get `responseType: blob`, you should use that. If not perform the [xmlhttprequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) with the $http service to avoid the memory overhead of converting the stream to arraybuffer. – georgeawg Jul 24 '18 at 19:33

0 Answers0