2017-09-29 66 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; 
} 
相關問題