【问题标题】:I want to upload to aws s3 bucket, but my code is showing this error我想上传到 aws s3 存储桶,但我的代码显示此错误
【发布时间】:2021-05-30 19:05:32
【问题描述】:

我正在尝试使用此代码上传到 aws s3 存储桶,但代码中所需的文件显示未定义,有人可以帮忙吗?

 app.post("/news_feed", async (req, res, next) => {
            const ID = "...";
            const SECRET = "...";
            const BUCKET_NAME = "iconpathbucket";

          const s3bucket = new AWS.S3({
               acessKeyId: ID,
               secretAcessKey: SECRET,
               Bucket: BUCKET_NAME,
          });

               var busboy = new Busboy({ headers: req.headers });

               busboy.on("finish", () => {
               console.log("upload finished");

              const file = req.body.image;
              console.log(file);

            const params = {
                 Bucket: BUCKET_NAME,
                 Key: file.name,
                 Body: file.data,
              };
              
            if (req.session.user_id) {
                s3bucket.upload(params, (err, data) => {
                if (err) {
                     console.log(err);
                } else {
                     console.log("This is the uploaded image", data);
                }
              });
            }
          });
        });

【问题讨论】:

  • 您究竟从哪里得到undefined?我看到你还有一个错字secretAcessKey - 应该是secretAccessKeyacessKeyId - 应该是accessKeyId

标签: javascript node.js amazon-s3


【解决方案1】:

您需要使用multer。它将处理请求并将文件/图像上传到 S3。

示例代码:

var multer  = require('multer')
var upload = multer({ dest: './public/data/uploads/' })
app.post('/news-feeds', upload.single('uploaded_file'), function (req, res) {
   // req.file is the name of your file in the form, here 'uploaded_file'
   // req.body will hold the text fields, if there were any 
   console.log(req.file, req.body)
});

【讨论】:

  • 非常感谢....我已经这样做了,但是我收到一个新错误,上面写着“参数中缺少必需的键 'Bucket'”,而我的参数看起来像这样 const uploadParams = { Bucket:bucketName,Body:fileStream,Key:file.filename }
  • @AfolabiOpeyemi 查看此stackoverflow.com/questions/51724886/…
猜你喜欢
  • 2015-12-07
  • 2020-02-20
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2022-01-09
  • 2018-08-26
相关资源
最近更新 更多