Created
October 5, 2022 22:54
-
-
Save Shaxadhere/e6be96887915d519b711e0d2f66eff5d to your computer and use it in GitHub Desktop.
upload object to wasabi cloud with multer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { S3Client } = require('@aws-sdk/client-s3'); | |
const AWS = require('aws-sdk'); | |
const multer = require('multer') | |
const multerS3 = require('multer-s3') | |
const ep = new AWS.Endpoint('s3.wasabisys.com'); | |
const { generateString } = require("./index") | |
const s3 = new S3Client({ | |
endpoint: ep, | |
region: process.env.WASABI_REGION, | |
credentials: { | |
accessKeyId: process.env.WASABI_ACCESS_KEY_ID, | |
secretAccessKey: process.env.WASABI_SECRET_ACCESS_KEY, | |
}, | |
sslEnabled: false, | |
s3ForcePathStyle: true, | |
signatureVersion: 'v4', | |
}); | |
module.exports = uploadToWasabi = multer({ | |
storage: multerS3({ | |
s3: s3, | |
bucket: process.env.bucketName, | |
contentType: multerS3.AUTO_CONTENT_TYPE, | |
acl: 'public-read', | |
metadata: function (req, file, cb) { | |
cb(null, { fieldName: file.fieldname }); | |
}, | |
key: function (req, file, cb) { | |
const fileName = generateString(18) + file.originalname.split(".").pop() | |
req.body.uploaded = fileName | |
cb(null, fileName); | |
}, | |
}), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment