Nodejs Amazon S3file upload

我试图创build一个表单上传我的本地文件到我的S3桶,但我有点困惑,上传逻辑应该存在的部分。 我想保持我的POST路线干净,并引用在一个单独的文件中的AWS逻辑,但我有点困惑,什么值应该用于参数级别的身体属性,以及我应该如何引用这个模块在fileAttachment行设置完成的上传URL到我的数据库属性。 任何人都可以指向正确的方向吗?

这是我的aws-s3.js文件:

 module.exports = function() { var path = require('path'); var async = require('async'); var fs = require('fs'); var AWS = require('aws-sdk'); var config = require(path.resolve(__dirname, '..', '..','./config/config.js')); AWS.config.region = config.region; var s3 = new AWS.S3({params: {Bucket: config.awsBucket}}); var params = {Key: config.awsAccessKeyId, Body: req.body.fileAttachment}; s3.upload(params, function(err, data){ if (err) { console.log("Error uploading data: ", err); } else { console.log("Successfully uploaded data to " + config.awsBucket + " /myKey"); } }); return s3; }; 

这是我的路线:

 appRoutes.route('/create/file') .get(function(req, res){ models.DiscoverySource.findAll({ where: { organizationId: req.user.organizationId }, attributes: ['discoverySource'] }).then(function(discoverySource){ res.render('pages/app/file-create.hbs',{ discoverySource: discoverySource }); }); }) .post(function(req, res){ models.File.create({ discovery: req.body.discovery, discoverySource: req.body.discoverySource, fileAttachment: userId: req.user.user_id }).then(function(){ res.redirect('/app'); }); }); 

形成:

 <form action="/app/create/file" method="post"> <div class="form-header"> <label for="data-discovery-source">Discovery Source:</label> <select name="discoverySource"> {{#each discoverySource}} <option value="{{this.discoverySource}}">{{this.discoverySource}}</option> {{/each}} </select> <label for="data-discovery">Discovery:</label> <textarea id="discovery-text-field" name="discovery"></textarea> <label for="report-link">File Attachment:</label> <input type="file" name="fileAttachment"> </div> <button type="submit" id="create-button">Create File</button> </form> 

你可以尝试使用MULTER-S3模块。

它允许您上传configuration为AWS的存储文件。

这段代码使用aws-sdk模块,更多关于它的configuration可以在这里find。

这是我的代码示例:

它在Node.js中使用推荐的用于JavaScript的亚马逊AWS开发工具包,并且还使用multer express中间件来上传文件。

 var aws = require('aws-sdk') var express = require('express') var multer = require('multer') var multerS3 = require('multer-s3') var app = express() var s3 = new aws.S3({ {accessKeyId: 'akid', secretAccessKey: 'secret'}) //this can also be configured in config file and through code var upload = multer({ storage: multerS3({ s3: s3, bucket: 'some-bucket', key: function (req, file, cb) { cb(null, Date.now().toString()) } }) }) //the upload.array -means that you can load multiple files(max 3) named photos maximum 3, the resulting AWS full path urls will be returned in req.files app.post('/upload', upload.array('photos', 3), function(req, res, next) { //reference results that can be stored in database for example in req.files res.send('Successfully uploaded ' + req.files.length + ' files!') }) 

这个代码还使用了multer npm模块。 更多关于它的configuration可能性,如:single,任何upload.array,字段可以在这里find。

您可以select使用Minio-js,这里是presigned-postpolicy.js一个例子。 我希望它有帮助。

 var Minio = require('minio') var s3Client = new Minio({ endPoint: 's3.amazonaws.com', accessKey: 'YOUR-ACCESSKEYID', secretKey: 'YOUR-SECRETACCESSKEY', insecure: false // Default is false. }) // Construct a new postPolicy. var policy = s3Client.newPostPolicy() // Set the object name my-objectname. policy.setKey("my-objectname") // Set the bucket to my-bucketname. policy.setBucket("my-bucketname") var expires = new Date expires.setSeconds(24 * 60 * 60 * 10) //10 days policy.setExpires(expires) policy.setContentLengthRange(1024, 1024*1024) // Min upload length is 1KB Max upload size is 1MB s3Client.presignedPostPolicy(policy, function(e, formData) { if (e) return console.log(e) var curl = [] curl.push('curl https://s3.amazonaws.com/my-bucketname') for (var key in formData) { if (formData.hasOwnProperty(key)) { var value = formData[key] curl.push(`-F ${key}=${value}`) } } // Print curl command to upload files. curl.push('-F file=@<FILE>') console.log(curl.join(' ')) }) 

免责声明:我为Minio开发了与AWS S3兼容的开源对象存储。