【问题标题】:nodejs s3 bucket upload script reusablenodejs s3存储桶上传脚本可重用
【发布时间】:2019-07-28 22:48:07
【问题描述】:

大家好,我正在尝试将文件上传到 s3,我可以使用以下代码成功完成此操作。但是我想让它更可重用。我希望能够使用这样的功能。

singleFileUpload(fieldName, bucketName, newfileName);

这样我就可以在多个页面上使用相同的功能,而无需一遍又一遍地定义它。我还希望它能够在 try catch 块中返回任何上传错误。这可能吗?这是我到目前为止有效的代码。

var AWS = require("aws-sdk");
var multer = require("multer");
multerS3 = require("multer-s3");
var fs = require("fs");
AWS.config.credentials = {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: "eu-west-1"
};
AWS.config.region = "eu-west-1";
var s3 = new AWS.S3();

var newFileName = "coolNewFileName";
      const fileFilter = (req, file, cb) => {
        var ext = file.originalname.split(".")[1];
        console.log("ext", ext);
        if (ext == "jpg" || ext == "mp4" || ext == "wmv") {
          cb(null, true);
        } else {
          cb(new Error("invalid file format"), false);
        }
      };

      var upload = multer({
        fileFilter,
        storage: multerS3({
          s3,
          bucket: process.env.BUCKET_NAME,
          acl: "public-read",
          metadata: function(req, file, cb) {
            cb(null, { fieldName: "testing_meta_data!" });
          },
          key: function(req, file, cb) {
            console.log(file);
            let fileExtension = file.originalname.split(".")[1];
            cb(null, newFileName + "." + fileExtension);
          }
        })
      });
      var singleUpload = upload.single("image");
      singleUpload(req, res, function(err) {
        if (err) {
          data.error = true;
          data.message = err.message;
          console.log(data);
          res.json(data);
        } else {
          // res.json({ imageurl: req.file.location });
          data.fileUploadLocation = req.file.location;
          console.log(data.fileUploadLocation);
        }
      });

这里的任何帮助都会非常感谢您。

【问题讨论】:

  • 你不需要multer,试着用S3 SDK来做,它有你需要的一切

标签: node.js amazon-s3 file-upload multer multer-s3


【解决方案1】:

这就是它如何将以下代码放在一个单独的文件中。 aws 文件包含 aws 和 s3 配置的包含。像这样:

    var AWS = require("aws-sdk");
    AWS.config.credentials = {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
      region: "eu-west-1"
    };
    AWS.config.region = "eu-west-1";

    var s3 = new AWS.S3();

    module.exports = { AWS, s3 };

var AWS = require("../AWS").AWS;
var s3 = require("../AWS").s3;
var multer = require("multer");
var multerS3 = require("multer-s3");

比我包含以下代码:

让 singleFileUpload = require("../upload");

最后我在 upload/index.js 文件中构建函数,如下所示:

function singleFileUpload(req, res, newFileName, bucketName, fieldName) {
  var fileFilter = (req, file, cb) => {
    var ext = file.originalname.split(".").slice(-1);
    console.log("ext", ext);
    if (ext == "jpg" || ext == "mp4" || ext == "wmv") {
      cb(null, true);
    } else {
      cb(new Error("invalid file format"), false);
    }
  };
  var upload = multer({
    fileFilter,
    storage: multerS3({
      s3,
      bucket: bucketName,
      acl: "public-read",
      metadata: function(req, file, cb) {
        cb(null, { test: "testing_meta_data!" });
      },
      key: function(req, file, cb) {
        console.log(file);
        let fileExtension = file.originalname.split(".")[1];
        cb(null, newFileName + "." + fileExtension);
      }
    })
  });
  var singleUpload = upload.single(fieldName);
  singleUpload(req, res, error => {
    if (error) {
      throw error;
    } else {
      console.log("it worked");
    }
  });
}

module.exports = singleFileUpload;

我比这样运行函数:

 var newFileName = "Testing";
  var fieldName = "image";
  singleFileUpload(
    req,
    res,
    newFileName,
    process.env.BUCKET_NAME,
    fieldName
  );

【讨论】:

    【解决方案2】:

    你可以让你的代码更安全,里面有这一行。 Aws-sdk 知道在哪里查找您的凭据。

    删除:

    Aws.config.credentials

    添加:

    const credentials = new AWS.SharedIniFileCredentials({profile: 'name-of-profile'});

    【讨论】:

      猜你喜欢
      • 2022-11-05
      • 2017-04-19
      • 2018-05-31
      • 2019-06-12
      • 2018-12-30
      • 2018-01-06
      • 1970-01-01
      • 2015-03-12
      • 2020-10-21
      相关资源
      最近更新 更多