如何使用nodeJS将单个文件上传到aws-s3上的多个路径? 问题:我有1个图像文件:images.jpg。现在,我想使用不同的路径将此文件上传到aws-s3。
答案 0 :(得分:0)
将文件上传到s3中的其他目录,提供您的accesskeyid和secretaccesskey
const fs = require('fs'),
AWS = require('aws-sdk'),
{promisify} = require('util')
const uploadFile =async (file_path, folder) => {
try {
var s3 = new AWS.S3({
accessKeyId: '',
secretAccessKey: ''
});
//reading the file and converting it into buffer
const readFile = promisify(fs.readFile).bind(fs);
const data =await readFile(file_path)
if (!data) throw "reading file failed";
const params = {
Bucket: 'testbucketnayan', // bucket name
Key: `${folder}/file.js`,
Body: data.toString()
};
const upload = promisify(s3.upload).bind(s3);
const up_data = await upload(params)
if (!up_data) throw "Upload failed"
console.log(up_data)
} catch(err) {
console.log(err);
}
}
上载位置具有目录名称。文件将上传到的位置。遍历数组并上传文件,可以使用Promise.all,它将提高性能。
const upload_location = ['abc', 'def'];
(async () => {
for (var folder of upload_location)
await uploadFile('resposetwi.js', folder);
})()
请使用您尝试的代码询问您的问题,这样可以更轻松地了解问题出在哪里。