如何使用NodeJS将文件上传到Amazon AWS3?

时间:2019-11-22 09:48:20

标签: node.js reactjs amazon-web-services amazon-s3 react-router

我正在尝试从正在处理的MERN应用程序中上传文件。我几乎完成了NodeJS后端部分。

上述应用程序将允许用户将图像(jpg,jpeg,png,gif等)上传到我创建的Amazon AWS S3存储桶中。

好吧,让我们这样说。我创建了一个助手:

const aws = require('aws-sdk');
const fs = require('fs');

// Enter copied or downloaded access ID and secret key here
const ID = process.env.AWS_ACCESS_KEY_ID;
const SECRET = process.env.AWS_SECRET_ACCESS_KEY;

// The name of the bucket that you have created
const BUCKET_NAME = process.env.AWS_BUCKET_NAME;

const s3 = new aws.S3({
  accessKeyId: ID,
  secretAccessKey: SECRET
});

const uploadFile = async images => {
  // Read content from the file
  const fileContent = fs.readFileSync(images);

  // Setting up S3 upload parameters
  const params = {
    Bucket: BUCKET_NAME,
    // Key: 'cat.jpg', // File name you want to save as in S3
    Body: fileContent
  };

  // Uploading files to the bucket
  s3.upload(params, function(err, data) {
    if (err) {
      throw err;
    }
    console.log(`File uploaded successfully. ${data.Location}`);
  });
};

module.exports = uploadFile;

该助手使用了我的三个环境变量,它们是存储桶的名称,keyId和秘密密钥。

从表单(最终将在前端添加)添加文件时,用户将能够发送多个文件。

现在,我当前的发布路线完全像这样:

req.body.user = req.user.id;
req.body.images = req.body.images.split(',').map(image => image.trim());
const post = await Post.create(req.body);

res.status(201).json({ success: true, data: post });

那右边很好用,但是把req.body.images作为字符串,每个图像用逗号隔开。将从Windows目录中选择的许多文件上传到AWS S3的正确方法是什么?我尝试这样做,但是没用:/

// Add user to req,body
req.body.user = req.user.id;
uploadFile(req.body.images);
const post = await Post.create(req.body);

res.status(201).json({ success: true, data: post });

谢谢,希望你们能帮助我解决这个问题。现在,我正在用Postman进行测试,但是稍后文件将通过表单发送。

2 个答案:

答案 0 :(得分:0)

好吧,您可以为每个文件多次调用uploadFile:

class public DoubleContainer extends Container {
    @Override
    public void addMultiple(List<Integer> numbers) {
        List<Integer> doubledNumbers = numbers.stream()
            .map(number -> number * 2)
            .collect(Collectors.toList());
        super addMultiple(doubledNumbers);
    }

    @Override
    public void addSingle(Integer number) {
        super addSingle(number * 2);
    }
}

在旁注中,您应按承诺更改S3.upload:

try{
    const promises= []
    for(const img of images) {
          promises.push(uploadFile(img))
        }
    await Promise.all(promises)
    //rest of logic

}catch(err){ //handle err }

奖金,如果您希望避免后端处理上传,可以使用aws s3 signed urls并让客户端浏览器处理,从而节省了服务器资源。

Post对象的另一件事应该只包含媒体的Urls,而不是媒体本身。

答案 1 :(得分:0)

// Setting up S3 upload parameters
    const params = {
        Bucket: bucket, // bucket name
        Key: fileName, // File name you want to save as in S3
        Body: Buffer.from(imageStr, 'binary'), //image must be in buffer
        ACL: 'public-read', // allow file to be read by anyone
        ContentType: 'image/png', // image header for browser to be able to render image
        CacheControl: 'max-age=31536000, public' // caching header for browser
    };

    // Uploading files to the bucket
    try {
        const result = await s3.upload(params).promise();
        return result.Location;
    } catch (err) {
        console.log('upload error', err);
        throw err;
    }