简体   繁体   English

使用节点js和Postman将文件上传到AWS S3

[英]Upload a file to AWS S3 using node js and Postman

I am new to AWS,and trying to figure out how to upload a file using the AWS S3 API ( http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html ), which is incorporated in my own api. 我是AWS的新手,并试图弄清楚如何使用AWS S3 API( http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html )上传文件,自己的API。

I can create a bucket, and get a list of all the buckets- however I am struggling with a file upload. 我可以创建一个存储桶,并获取所有存储桶的列表,但是我正在努力上传文件。

This is by code: 这是通过代码:

router.post('/upload', function(req, res, next) {
    var params = {
        Bucket: req.body.bucketName,
        Key: req.body.key,
        Body: req.body.body
    }
    s3.putObject(params, function(err, data) {
        if (err) {
            return next(err)
        } else {
            res.json(data)
        }
    })
})

So when I run my server, I try to make a post request using postman to localhost:8080/upload with the following: attaching a file, and the key and body - but I think I do this part wrong. 因此,当我运行服务器时,我尝试使用邮递员向localhost:8080/upload发出以下请求:附加文件,密钥和主体-但我认为这部分做错了。 邮递员视图

And I also attach the file: 而且我还附加了文件:

在此处输入图片说明

Question is: 问题是:

Do I correctly understand the following- Bucket = the bucket name I want to upload to, Key = the file name, Body = the file contents? 我是否正确理解以下内容: Bucket =我要上传到的桶名称, Key =文件名, Body =文件内容?

If yes, how do I get this to upload to S3 bucket, as with the current code I get a file added to s3 called ' text.txt ' with the contents ' heello ' rather than my ' test.txt ' file. 如果是,如何将其上传到S3存储桶,就像当前代码一样,我将添加到s3的文件名为text.txt ,其内容为heello而不是test.txt文件。

are you trying to upload a file correct? 您是否要正确上传文件? So you should use a multipart/form-data content-type, and in body you can point to your file buffer. 因此,您应该使用multipart / form-data内容类型,并且在正文中您可以指向文件缓冲区。

In my case, I use with swagger: 就我而言,我大摇大摆地使用:

upload: (req, res) => {
  const params = {
    Bucket: 'bucket-name',
    Key: req.swagger.params.file.value.originalname,
    ACL: 'public-read',
    Body: req.swagger.params.file.value.buffer
  };

  s3.putObject(params, function(err, data) {
    if (err) {
      console.log('Error uploading image: ', err);
      res.status(500).json().end();
    } else {
      res.status(200).json('File is uploaded').end();
    }
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM