我正在尝试创建一个将我的本地文件上传到我的S3存储桶的表单,但是我对上传逻辑的哪些部分应该存在感到有些困惑。我希望保持我的POST路由清洁并引用位于单独文件中的AWS逻辑,但是我对于params级别的Body属性应该使用什么值以及我应该如何引用它感到困惑。将完成的上载URL设置为fileAttachment
行的数据库属性时的模块。有人能指出我正确的方向吗?
这是我的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>
答案 0 :(得分:2)
您可以尝试使用multer-s3模块。
它允许您上传配置为AWS的存储文件。
此代码使用aws-sdk模块,可以找到更多关于它的配置here。
这是我的代码示例:
它在Node.js中使用推荐的amazon AWS SDK for JavaScript 它还使用multer表达中间件来上传文件。
此代码也使用multer npm模块。有关其配置可能性的更多信息,例如:single,any upload.array,可以找到here字段。
答案 1 :(得分:2)
您也可以使用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兼容的开源对象存储。