如何将图片从我的服务器上传至数字海洋空间?

问题描述 投票:0回答:1

我是一个完全陌生的云存储上传.我想先在我的服务器上上传一张图片,然后我想把它上传到数字海洋空间(云存储),我正在用这个方法试一下

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 });
}; 

先谢谢你的帮助

node.js amazon-s3 digital-ocean cdn cloud-storage
1个回答
0
投票

随着一点点更多的研究,我得到了解决这个问题的办法。

 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);
            });
        });
© www.soinside.com 2019 - 2024. All rights reserved.