Search code examples
javascriptamazon-s3sinonnockaws-sdk-mock

getObject mock is returning 0 bytes file


getObject mock is returning 0 bytes file. My mock requirement is to get the complete file.

I have code as follows:

AWSmock.mock("S3", "getObject", (params: any, callback: any) => {
      try {

        const filePath = path.join(__dirname, "../../seed/dummy_model/SampleNames.zip");
        logger.debug("In mock S3.getObject()");
        const data = readFileSync(filePath);
        const stat = statSync(filePath);
        logger.verbose(JSON.stringify(stat));
        const props = {
          Body: data,
          ContentLength: data.length,
          ETag: '"' + createHash("md5").update(data).digest("hex") + '"',
          Key: params.Key,
          LastModified: stat.mtime,
        };
        logger.verbose(JSON.stringify(props));
        callback(null, props);
      } catch (err) {
        logger.error(err);
        // process.exit(1);
        callback(new Error("Unable to stream file."));
      }
    });

Is this an issue with the library or I am doing it wrong?
Please help me with this. Thanks in advance.


Solution

  • Yes, you are using it wrong. Try the below :

    AWSmock.mock("S3", "getObject", new Buffer(data));