如何使用节点js将存储为缓冲区的多个文件上传到s3存储桶

时间:2017-03-27 04:34:49

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

我已经将zip文件解压缩到buffer.Now我需要使用节点js将那些解压缩的文件从缓冲区上传到S3存储桶.Kindly帮我继续。

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助,只需循环解压缩文件并发送每个

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


module.exports.UploadFile = function(credentials, BucketPath, folder, Region, fname, s3url, req, res, callback) {
        // AWS Region
        AWS.config.region = Region;
        // AWS credentials
        AWS.config.update(credentials);

        // create read stream for file
        var filestream = fs.createReadStream(fname);
        //create an instance of s3
        var s3 = new AWS.S3({params: {Bucket: BucketPath}});
        // read file
        fs.readFile(fname, function (err, data) {
        //upload to s3
        s3.upload({ Key: folder + fname
                  , Body: filestream
                    // change contentType as needed
                  , ACL: 'public-read','ContentType' : 'application/pdf'}
                      , function(err,result) {
                       if(err) throw err;
                       // check if the file uploaded or not
                       s3.getObject({ Key: folder + fname },
                        function (error, data) {
                        if (error != null) {

                        } else {
                            // remove file from local if needed
                            fs.unlink(fname, function (err) {
                           if (err) {
                               console.log(err);
                           }
                           callback("", data);

                       });
                       }
                    });

           });
        });
};