【问题标题】:Uploading multiple files to AWS S3 using NodeJS使用 NodeJS 将多个文件上传到 AWS S3
【发布时间】:2017-09-25 12:55:06
【问题描述】:

我正在尝试使用 NodeJS 将我目录中的所有文件上传到我的 S3 存储桶。如果我明确地为Key: 字段提供文件路径+文字字符串,我可以一次上传一个文件。

下面是我正在使用的脚本:

var AWS = require('aws-sdk'),
    fs = require('fs');

// For dev purposes only
AWS.config.update({ accessKeyId: '...', secretAccessKey: '...' });

// reg ex to match
var re = /\.txt$/;

// ensure that this file is in the directory of the files you want to run the cronjob on

// ensure that this file is in the directory of the files you want to run the cronjob on
fs.readdir(".", function(err, files) {
    if (err) {
    console.log( "Could not list the directory.", err)
    process.exit( 1 )
    }


    var matches = files.filter( function(text) { return re.test(text) } )
    console.log("These are the files you have", matches)
    var numFiles = matches.length


    if ( numFiles ) {
        // Read in the file, convert it to base64, store to S3

        for( i = 0; i < numFiles; i++ ) {
            var fileName = matches[i]

            fs.readFile(fileName, function (err, data) {
                if (err) { throw err }

                // Buffer Pattern; how to handle buffers; straw, intake/outtake analogy
                var base64data = new Buffer(data, 'binary');


                var s3 = new AWS.S3()
                    s3.putObject({
                       'Bucket': 'noonebetterhaventakenthisbucketnname',
                        'Key': fileName,
                        'Body': base64data,
                        'ACL': 'public-read'
                     }, function (resp) {
                        console.log(arguments);
                        console.log('Successfully uploaded, ', fileName)
                    })
            })

        }

    }

})

它会为每个尝试上传到 S3 的文件产生此错误:

These are the files you have [ 'test.txt', 'test2.txt' ]
{ '0': null,
  '1': { ETag: '"2cad20c19a8eb9bb11a9f76527aec9bc"' } }
Successfully uploaded,  test2.txt
{ '0': null,
  '1': { ETag: '"2cad20c19a8eb9bb11a9f76527aec9bc"' } }
Successfully uploaded,  test2.txt

编辑:使用变量名更新以允许读取密钥而不是matches[i]

为什么它只上传test2.txt,我如何让它上传我的matches变量中的每个文件?

【问题讨论】:

  • 你没有得到键值。 决定关键值是什么。您希望将上传的对象存储在 S3 存储桶中的哪个键?
  • 我的本地机器上显示的相同文件名@jarmod

标签: node.js amazon-web-services amazon-s3


【解决方案1】:

引用此Asynchronously reading and caching multiple files in nodejs 以获得解决方案。

tl;dr 范围问题 - 需要在闭包中包装变量;可以通过为 readFiles3.putObject 创建一个函数并在 for 循环中调用它来做到这一点。

var AWS = require('aws-sdk'),
    fs = require('fs');

// For dev purposes only
AWS.config.update({ accessKeyId: '...', secretAccessKey: '...' });

var s3 = new AWS.S3()

function read(file) {
    fs.readFile(file, function (err, data) {
        if (err) { throw err }

        // Buffer Pattern; how to handle buffers; straw, intake/outtake analogy
        var base64data = new Buffer(data, 'binary');

        s3.putObject({
           'Bucket': 'noonebetterhaventakenthisbucketnname',
            'Key': file,
            'Body': base64data,
            'ACL': 'public-read'
         }, function (resp) {
            console.log(arguments);
            console.log('Successfully uploaded, ', file)
        })
    })
}

// reg ex to match
var re = /\.txt$/;

// ensure that this file is in the directory of the files you want to run the cronjob on
fs.readdir(".", function(err, files) {
    if (err) {
        console.log( "Could not list the directory.", err)
        process.exit( 1 )
    }

    var matches = files.filter( function(text) { return re.test(text) } )
    console.log("These are the files you have", matches)
    var numFiles = matches.length


    if ( numFiles ) {
        // Read in the file, convert it to base64, store to S3

        for( i = 0; i < numFiles; i++ ) {
            read(matches[i])
        }

    }

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 2020-01-29
    • 2019-12-13
    • 2016-10-17
    • 1970-01-01
    • 2015-03-17
    • 2020-07-26
    • 2016-08-07
    相关资源
    最近更新 更多