我是一个完全陌生的云存储上传.我想先在我的服务器上上传一张图片,然后我想把它上传到数字海洋空间(云存储),我正在用这个方法试一下
router.post('/upload', MEDIA.addPostMedia, MEDIA.afterPostMediaUpload);
*** In another file ********
module.exports.addPostMedia = multer({ storage: postMediaStorage }).array('files', 5);
module.exports.afterPostMediaUpload = function (req, res, next) {
let filesArr = [];
req.files.forEach(file => {
compress_local_image(file, file1 => { });
imageOrientationFixSync(file.path);
s3.upload({
s3: s3,
bucket: `cdn.******.com`,
acl: 'public-read',
key: file.path
})
let finalResponse = {
mimetype: file.mimetype,
filename: file.filename,
path: file.path.replace(SITE_CONFIG.mediaBasePath, "uploads/")
};
filesArr.push(finalResponse);
});
res.json({ status: true, data: filesArr });
};
先谢谢你的帮助
随着一点点更多的研究,我得到了解决这个问题的办法。
fs.readFile(file.path, function (err, data) {
if (err) throw err;
const params = {
Bucket: `cdn.*****.com`, // pass your bucket name
Key: file.path, // file will be saved as testBucket/contacts.csv
Body: data,
ACL: 'public-read'
};
s3.upload(params, function (error, data) {
if (error) {
console.log(error);
}
console.log(data.Location);
});
});