1

Got to do a crc32 checksum for a uploaded file before saving to s3 disk. I am using multer and multer-s3 to upload a file to s3 which was easy to achive. Here In middle trying to do a crc32 check during file upload. Which was partially completed The below code does the job of crc32 check perfectly but the uploaded file is not having all the chunks.

var AWS = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
const { crc32 } = require('crc');

AWS.config.update({
  secretAccessKey: AWS_XXXXXX_SecretAccessKey,
  accessKeyId: AWS_XXXXXX_AccessKeyID,
  //region: 'us-east-1'
});

const s3 = new AWS.S3();

var upload = multer({
  limits: { fileSize: 100000 },
  storage: multerS3({
    s3: s3,
    bucket: constants.AWS_Cred.bucketName,
    /* metadata: function (req, file, cb) {
        cb(null, {fieldName: file.fieldname});
        //tried moving stream logic here still no success
      }, */
    key: function (req, file, cb) {
      console.log("KEY");
      var buffer = new Buffer.alloc(0);
      // const b = file.stream.pipe( new PassThrough());  -- tried to clone a stream here still failed
      file.stream.on("data", function(data) {
        buffer = Buffer.concat([buffer, data]);
      }); 
      file.stream.on('end', function() {
        let fileCRCCheckSum = crc32(buffer).toString(16);
        console.log(fileCRCCheckSum);
      });
      cb(null, constants.Firmware_Saved_Location + file.originalname); 
    },
  }),
});



Router
router.post(
  "/uploadSong",
  upload.single("files"),
  uploadController.DoesSomeDBEntriesHere
);

Will Take care of Controller which just saves other Entries to Db. Happy to Hear Any Suggestions.

Purushoth.Kesav
  • 615
  • 7
  • 16

0 Answers0