Node.js nodejsamazons3文件上传
我试图创建一个表单,将本地文件上传到S3存储桶,但对于上传逻辑的部分应该存在于何处,我有点困惑。我想保持我的POST路由干净并引用单独文件中包含的AWS逻辑,但我有点困惑,在Node.js nodejsamazons3文件上传,node.js,amazon-s3,module,Node.js,Amazon S3,Module,我试图创建一个表单,将本地文件上传到S3存储桶,但对于上传逻辑的部分应该存在于何处,我有点困惑。我想保持我的POST路由干净并引用单独文件中包含的AWS逻辑,但我有点困惑,在文件附件行将已完成的上载URL设置为我的数据库属性时,应该为params级别的Body属性使用什么值,以及应该如何引用此模块。谁能给我指出正确的方向吗 这是我的aws-s3.js文件: module.exports = function() { var path = require('path'); var async =
文件附件
行将已完成的上载URL设置为我的数据库属性时,应该为params级别的Body属性使用什么值,以及应该如何引用此模块。谁能给我指出正确的方向吗
这是我的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');
});
});
表格:
发现来源:
{{#每个发现源}
{{this.discoverySource}
{{/每个}}
发现:
文件附件:
创建文件
您可以尝试使用multer-s3模块
它允许您上传配置为AWS的storaage文件
此代码使用aws sdk模块,可以找到有关其配置的更多信息
下面是我的代码示例:
它使用Node.js中推荐的amazon AWS SDK for JavaScript,并使用
它还使用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模块。有关其配置可能性的更多信息,如:单个、任意upload.array、字段,可以找到。您也可以使用这里的一个示例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(' '))
})
var Minio=require('Minio'))
var s3Client=新的Minio({
端点:“s3.amazonaws.com”,
accessKey:'YOUR-ACCESSKEYID',
secretKey:'YOUR-SECRETACCESSKEY',
不安全:false//默认值为false。
})
//构建新的后策略。
var policy=s3Client.newPostPolicy()
//将对象名设置为my objectname。
policy.setKey(“我的objectname”)
//将桶设置为我的bucketname。
政策。挫折(“我的bucketname”)
var expires=新日期
过期。设置秒(24*60*60*10)//10天
policy.setExpires(过期)
policy.setContentLengtRange(1024,1024*1024)//最小上载长度为1KB最大上载大小为1MB
s3Client.presignedPostPolicy(策略、功能(e、formData){
if(e)返回console.log(e)
var curl=[]
卷曲https://s3.amazonaws.com/my-bucketname')
for(formData中的var键){
if(formData.hasOwnProperty(键)){
var值=formData[键]
push(`F${key}=${value}`)
}
}
//打印curl命令以上载文件。
curl.push('-F file=@'))
console.log(curl.join(“”))
})
免责声明:我为与AWS S3兼容的开源对象存储工作 谢谢分享,但这里似乎没有太多的文档或与我的问题重叠。在哪里设置访问密钥ID?
upload.array()
参数代表什么?如何将结果上传的URL引用到我的数据库文件附件
?更新了我的答案。
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(' '))
})