Reduced file size after uploading to S3 bucket from nodejs

Viewed 483

I am trying to upload files from a particular folder location to a sample S3 bucket. I am using standard nodejs aws-sdk for this. Files are deepzoom images (.dzi) files. Files are getting uploaded to my S3 bucket but the contents of the file are not getting uploaded properly. Like I am uploading images of sizes 800B, but after uploading the size of image is only 7B. I tried downloading it to see its content but the file doesn't contains the image but just the file name. This is the code I am running for uploading files:

function read(file, numFiles) {
fs.readFile(file, function (err, data) {
  if (err) console.log(err);
  const fileContent = Buffer.from(file, "binary");
  s3.putObject(
    {
      Bucket: "sample-bucket",
      Key: file,
      Body: fileContent,
    },
    function (resp) {
      console.log(arguments);
      console.log("Successfully uploaded, ", file);
      uploadCount++;
      console.log("uploadcount is:", uploadCount);
      if (uploadCount == numFiles) {
        res.send("All files uploaded");
      }
    }
  ).on("httpUploadProgress", (evt) => {
    console.log(`Uploaded ${evt.loaded} out of ${evt.total}`);
  });
});

}

I am passing files to this read function from another function. I am not sure why this is happening. Any help would be appreciated.

Before uploading image properties:

enter image description here

Property of image uploaded to S3 bucket:

enter image description here

1 Answers

Buffer.from(file) will not return the content of the file but return the buffer of the argument, this time the argument is "file". So the file uploaded to S3 has filename as contents.

Try to change this line

const fileContent = Buffer.from(file, "binary");

to like this.

const fileContent = fs.readFileSync(file);
Related