2017-09-29 27 views
1

我正在使用此代码在互联网上找到要将多个文件上传到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。

+0

你为什么不使用res作为通知来运行另一个进程? – SILENT

+0

每次上传新文件时都会调用res。 – doej

+1

那么?所有上传完成后,您的提问是否询问有关通知? – SILENT

回答

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

    }) 

}) 

... 
+0

我的代码缺失,我在for循环中打开multuiple文件夹。我不知道文件的数量。有没有解决方法? – doej

+0

@doej当你调用files.length时,你确实知道......或者只是给for循环添加一个计数器 – SILENT

0

你可以尝试使用的承诺和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; 
} 
相关问题