NodeJS 如何指定AWS-S3存储桶的multer-S3中的上载目录?

k5hmc34c  于 2022-12-22  发布在  Node.js
关注(0)|答案(4)|浏览(149)

我正在使用express + multer-s3将文件上传到AWS S3服务。
使用下面的代码,我可以将文件上传到S3Bucket,但直接上传到Bucket中。
我想把它们上传到桶里的一个文件夹里。
我无法找到这样做的选择。
下面是代码

AWS.config.loadFromPath("path-to-credentials.json");
var s3 = new AWS.S3();

var cloudStorage = multerS3({
    s3: s3,
    bucket: "sample_bucket_name",
    contentType: multerS3.AUTO_CONTENT_TYPE,
    metadata: function(request, file, ab_callback) {
        ab_callback(null, {fieldname: file.fieldname});
    },
    key: function(request, file, ab_callback) {
        var newFileName = Date.now() + "-" + file.originalname;
        ab_callback(null, newFileName);
    },
});
var upload = multer({
    storage: cloudStorage
});

router.post("/upload", upload.single('myFeildName'), function(request, response) {
    var file = request.file;
    console.log(request.file);
    response.send("aatman is awesome!");
});
r6hnlfcb

r6hnlfcb1#

S3并不总是有文件夹(参见http://docs.aws.amazon.com/AmazonS3/latest/UG/FolderOperations.html),它会通过在文件名中添加一个用/分隔的字符串来模拟文件夹。
例如:

key: function(request, file, ab_callback) {
    var newFileName = Date.now() + "-" + file.originalname;
    var fullPath = 'firstpart/secondpart/'+ newFileName;
    ab_callback(null, fullPath);
},
slwdgvem

slwdgvem2#

我的动态目标路径的解决方案。希望这对大家有帮助!

const fileUpload = function upload(destinationPath) {
  return multer({
    fileFilter: (req, file, cb) => {
      const isValid = !!MIME_TYPE_MAP[file.mimetype];
      let error = isValid ? null : new Error("Invalid mime type!");
      cb(error, isValid);
    },
    storage: multerS3({
      limits: 500000,
      acl: "public-read",
      s3,
      bucket: YOUR_BUCKET_NAME,
      contentType: multerS3.AUTO_CONTENT_TYPE,
      metadata: function (req, file, cb) {
        cb(null, { fieldName: file.fieldname });
      },
      key: function (req, file, cb) {
        cb(null, destinationPath + "/" + file.originalname);
      },
    }),
  });
};


module.exports = fileUpload;

如何致电:

router.patch(
  "/updateProfilePicture/:userID",
  fileUpload("user").single("profileimage"),
  usersControllers.updateProfilePicture
);

“profile image”是传递到主体中的文件的键。
“user”是目标文件夹的路径。2你可以传递任何路径,包括文件夹和子文件夹。3这样我的文件就被放到了我的bucket里一个叫“user”的文件夹里。

yjghlzjz

yjghlzjz3#

还有另一种很好的方法!在你写好你的桶名之后,在那里加上文件夹名,在前面加一个斜杠。

const multerS3Config = multerS3({
    s3: s3,
    bucket: process.env.AWS_BUCKET_NAME + '/<your-folder-name>', //put a slash infront
    metadata: function (req, file, cb) {
        cb(null, { fieldName: file.fieldname });
    },
    key: function (req, file, cb) {
    cb(null, Date.now() + file.originalname)
    }  
});
k3fezbri

k3fezbri4#

为了通过指定动态目录来上传文件,我创建了一些功能/中间件,它们运行得非常好。
已重写存储函数以存储动态目标。

const storage = (path = '') => multerS3({
    acl: "public-read",
      s3,
      bucket: YOUR_BUCKET_NAME,
      contentType: multerS3.AUTO_CONTENT_TYPE,
      metadata: function (req, file, cb) {
        cb(null, { fieldName: file.fieldname });
      },
      key: function (req, file, cb) {
        const newFileName = Date.now() + "-" + file.originalname;
        const fullPath = path + '/' + newFileName;
        cb(null, fullPath); //use Date.now() for unique file keys
      },
})

新创建的中间件

const singleUpload = (name = 'document',dest = '') => {
  const uploads = multer({ storage: storage(dest) })
  return uploads.single(name)
}

const arrayUpload = (name = 'documents',dest = '') => {
  const uploads = multer({ storage: storage(dest) })
  return uploads.array(name)
}

const fieldUpload = (obj =[ {name: 'document', maxCount: 1}],dest = '') => {
  const uploads = multer({ storage: storage(dest) })
  return uploads.fields(obj)
}

调用中间件的并在参数中指定目的地,然后它将上传该特定目录中的数据。

相关问题