异步任务完成时收到通知

时间:2017-09-29 17:21:05

标签: javascript node.js amazon-s3

我正在使用我在互联网上找到的代码将多个文件上传到Amazon S3服务器。

const AWS = require("aws-sdk"); // from AWS SDK
const fs = require("fs"); // from node.js
const path = require("path"); // from node.js

// configuration
const config = {
    s3BucketName: 'your.s3.bucket.name',
    folderPath: '../dist' // path relative script's location
};

// initialize S3 client
const s3 = new AWS.S3({ signatureVersion: 'v4' });

// resolve full folder path
const distFolderPath = path.join(__dirname, config.folderPath);

// get of list of files from 'dist' directory
fs.readdir(distFolderPath, (err, files) => {

    if(!files || files.length === 0) {
        console.log(`provided folder '${distFolderPath}' is empty or does not exist.`);
        console.log('Make sure your project was compiled!');
        return;
    }

    // for each file in the directory
    for (const fileName of files) {

    // get the full path of the file
        const filePath = path.join(distFolderPath, fileName);

        // ignore if directory
        if (fs.lstatSync(filePath).isDirectory()) {
            continue;
        }

        // read file contents
        fs.readFile(filePath, (error, fileContent) => {
            // if unable to read file contents, throw exception
            if (error) { throw error; }

            // upload file to S3
            s3.putObject({
                Bucket: config.s3BucketName,
                Key: fileName,
                Body: fileContent
            }, (res) => {
                console.log(`Successfully uploaded '${fileName}'!`);
            });
        });
    }
});

如何通知上传已完成以执行其他流程?成功上传单个文件时调用res。

2 个答案:

答案 0 :(得分:0)

如何在文件上传时递增计数器,然后检查是否已上传所有文件:

...

var uploadCount = 0

// Read file contents
fs.readFile(filePath, (error, fileContent) => {

  // If unable to read file contents, throw exception
  if (error) { throw error }

  // Upload file to S3
  s3.putObject({
    Bucket: config.s3BucketName,
    Key: fileName,
    Body: fileContent
  }, (res) => {
    console.log(`Successfully uploaded '${fileName}'!`)

    // Increment counter
    uploadCount++

    // Check if all files have uploaded
    // 'files' provided in callback from 'fs.readdir()' further up in your code
    if (uploadCount >= files.length) {
      console.log('All files uploaded')
    }

  })

})

...

答案 1 :(得分:0)

您可以尝试使用promises和promise.all

const AWS = require("aws-sdk"); // from AWS SDK
const fs = require("fs"); // from node.js
const path = require("path"); // from node.js

// configuration
const config = {
  s3BucketName: 'your.s3.bucket.name',
  folderPath: '../dist' // path relative script's location
};

// initialize S3 client
const s3 = new AWS.S3({ signatureVersion: 'v4' });

// resolve full folder path
const distFolderPath = path.join(__dirname, config.folderPath);

// get of list of files from 'dist' directory
fs.readdir(distFolderPath, (err, pathURLS) => {
  if(!pathURLS || pathURLS.length === 0) {
    console.log(`provided folder '${distFolderPath}' is empty or does not exist.`);
    console.log('Make sure your project was compiled!');
    return;
  }
  let fileUploadPromises = pathURLS.reduce(uplaodOnlyFiles, []);
  //fileUploadPromises.length should equal the files uploaded
  Promise.all(fileUploadPromises)
      .then(() => {
          console.log('All pass');
      })
      .catch((err) => {
          console.error('uploa Failed', err);
      });
});

function uploadFileToAWS(filePath) {
    return new Promise(function (resolve, reject) {
        try {
            fs.readFile(filePath, function (err, buffer) {
                if (err) reject(err);
                // upload file to S3
                s3.putObject({
                    Bucket: config.s3BucketName,
                    Key: filePath,
                    Body: buffer
                }, (res) => {
                    resolve(res)
                });
            });
        } catch (err) {
            reject(err);
        }
    });
}

function uplaodOnlyFiles(fileUploadPromises, pathURL) {
    const fullPathURL = path.join(distFolderPath, pathURL);
    if (!fs.lstatSync(fullPathURL).isDirectory()) {
        fileUploadPromises.push(uploadFileToAWS(fullPathURL));
    }
    return fileUploadPromises;
}