使用绝对路径将文件上传到AWS S3存储桶

时间:2020-07-13 12:10:31

标签: javascript node.js express amazon-s3 file-upload

我正在尝试将文件从Express ejs上载到AWS s3存储桶,但我这样做是成功的,但是当我尝试从index.js所在的其他目录或文件夹中选择文件时,它不接受该文件并找不到错误文件。

index.js



'use strict';

const express = require('express');
const app = express();
const multer  = require('multer');
const multerS3 = require('multer-s3');
const AWS = require('aws-sdk');
const bodyParser = require('body-parser');

const s3 = new AWS.S3({
    accessKeyId: '',
    secretAccessKey: ''
});

app.use(bodyParser.urlencoded({extended : true})); 
app.set('view engine', 'ejs'); 



const uploadS3 = multer({
  storage: multerS3({
    s3: s3,
    bucket: '',
    metadata: (req, file, cb) => {
      cb(null, {fieldName: file.fieldname})
    },
    key: (req, file, cb) => {
      cb(null, Date.now().toString() + '-' + file.originalname)
    }
  })
});

test.ejs

<html>
  <form method="post" action="/upload">
    <input type="file" name="file" />
    <input type="submit" />
  </form>
</html>

route.js


var fileupload = require('../../common/service/file-upload');
//some code in between 

app.post('/upload', fileupload.uploadS3.single('file'),(req, res) => {
  console.log(req.file);
});


2 个答案:

答案 0 :(得分:0)

伙计,Express如何通过路径读取客户端的文件?在POST请求中上传/发送文件。

使用一些支持multipart/form-data文件上传的正文解析器(如mutler)来解析请求中的文件并将其上传到S3。

例如...

html

<html>
  <!-- don't forget enctype parameter -->
  <form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
  </form>
</html>
手动使用AWSmulter

节点服务器

const multer = require('multer'),
      AWS = require('aws-sdk'),
      S3 = new AWS.S3({
        ...
      });

router.post('/', multer().single('file'), (req, res) => {
  // debug req.file
  console.log(req.file);
  
  S3.upload({
    Bucket: '...',
    Key: req.file.originalname, 
    Body: req.file.buffer
  }, (err, res) => {
    if (err) throw err;
    res.json({
      message: "File uploaded to S3"
    });
  });
});

节点服务器,使用multer-s3

const multer = require('multer'),
      multerS3 = require('multer-s3'),
      AWS = require('aws-sdk'),
      S3 = new AWS.S3({
        ...
      });

let upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: '...',
    key: (req, file, cb) 
      => cb(null, file.originalname), // or whatever Key you like
  })
});

router.post('/', upload.single('file'), (req, res) => {
  res.json({
    message: "File uploaded to S3"
  });
});

答案 1 :(得分:0)

无论何时上传文件,都需要添加属性以形成enctype =“ multipart / form-data”形式。 。那么它将起作用。 使用multer-s3软件包将文件上传到s3,该软件包配置简单 参考:https://www.npmjs.com/package/multer-s3

所以您的test.ejs文件将是这样

   <html>
     <form method="post" action="/upload" enctype="multipart/form-data">
       <input type="file" name="file" />
      <input type="submit" />
      <const/form>
   </html>
   

和index.js

    const express = require('express');
    const bodyParser = require('body-parser');
    const AWS = require('aws-sdk');
    const multer = require('multer')
    const multerS3 = require('multer-s3')



    const app = express();
    const s3 = new AWS.S3({
        accessKeyId: //aws access key ,
        secretAccessKey: //aws secret key
    });

        const upload = multer({
          storage: multerS3({
            s3: s3,
            bucket: 'some-bucket',
            metadata: function (req, file, cb) {
              cb(null, {fieldName: file.fieldname});
            },
            key: function (req, file, cb) {
              cb(null, Date.now().toString())
            }
          })
        })



    app.post('/', upload.single('file'), (req, res) => {
      res.json({
        message: "File uploaded to S3"
      });
    });