在asynchronous任务完成时收到通知

我正在使用我在互联网上find的代码将多个file upload到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。

如何在file upload时增加计数器,然后检查是否所有的文件已经上传:

 ... 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') } }) }) ... 

你可以尝试使用promise和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; }