AWS s3 put

    0

    0

    The code creates a new AWS.S3 object and sets the following properties:

    accessKeyId: ACCESS_KEY secretAccessKey: SECRET_KEY

    The code then creates a memoryStorage object and sets the following properties:

    destination: function (req, file, callback) {

    callback(null, ""); }

    The code then creates a upload object and sets the following properties:

    storage: multer.memoryStorage({

    destination: function (req, file, callback) {

    callback(null, ""); }, }

    )

    The code then creates a router object and sets the following route:

    post("/image-upload", upload, (req, res) => {

    let fileName = req.file.originalname.split(".");

    const myFileType = fileName[fileName.length - 1];

    const Key = shopLogo/${uuidv4()}.${myFileType};

    console.log(fileName, myFileType, Key, BUCKET);

    const params = {

    Bucket: BUCKET,

    const s3 = new AWS.S3({
      accessKeyId: ACCESS_KEY,
      secretAccessKey: SECRET_KEY,
    });
    
    const storage = multer.memoryStorage({
      destination: function (req, file, callback) {
        callback(null, "");
      },
    });
    
    const upload = multer({ storage }).single("image");
    
    router.post("/image-upload", upload, (req, res) => {
      let fileName = req.file.originalname.split(".");
      const myFileType = fileName[fileName.length - 1];
      const Key = `shopLogo/${uuidv4()}.${myFileType}`;
      console.log(fileName, myFileType, Key, BUCKET);
      const params = {
        Bucket: BUCKET,
        Key,
        Body: req.file.buffer,
      };
    
      s3.upload(params, (err, data) => {
        if (err) {
          res.status(500).send(err);
        }
        res.status(200).send(data);
      });
    });
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.