DanielHindi / aws-s3-zipper

takes an amazon s3 bucket folder and zips it for streaming or serializes to a file
119 stars 74 forks source link

Fix zipping large number of files issue #38

Open dany611 opened 4 years ago

dany611 commented 4 years ago

This issue https://github.com/DanielHindi/aws-s3-zipper/issues/26 has been fixed. @DanielHindi @andrewsomething @ggoforth @relly @ygalbel

trongpham-itr commented 4 years ago

I found some bug, I fixed it, you can check again. Thanks @dany611

    this.getFiles(params, async (err, clearedFiles) => {
      if (err) console.error(err);
      else {
        const { files } = clearedFiles;
        let filesList = [];
        const batchSize = 10;

        let filesValid = [];

        while (((filesList = files.splice(0, batchSize)).length) !== 0) {
          const promises = _.map(filesList, async f => new Promise((resolve, reject) => {
            this.s3bucket.getObject({ Bucket: this.awsConfig.bucket, Key: f.Key }, (error, data) => {
              if (error) {
                console.log(error);
                reject(error);
              } else {
                const name = this.calculateFileName(f);
                if (name === '') {
                  resolve(f);
                } else {
                  console.log('zipping ', name, '...');
                  zip.append(data.Body, { name });
                  resolve(f);
                }
              }
            });
          }));

          const result = await Promise.all(promises);
          filesValid = _.concat(filesValid, result);
        }
        zip.manifest = filesValid;
        zip.on('finish', () => {
          callback(err, {
            zip,
            zippedFiles: filesValid,
            totalFilesScanned: clearedFiles.totalFilesScanned,
            lastScannedFile: clearedFiles.lastScannedFile,
          });
        });
        zip.finalize();
      }
    });