node.jsgzipamazon-advertising-api

Amazon Advertising API - Download Report Data and Unzip in NodeJS


I am trying to call the amazon advertising api and download a campaign report, successfully able to do the following so far:

Create a campaign report, get the report id and using the report id, I am able to get the s3 download url.

The problem is that when I try to download the data - the response returned by the api is supposed to be gunzipped json and comes in the format:

��VJN�-H�L��LQ�2227��45�017�����b%+��܂������< ߤ�+R<

How do I extract the json data from this compressed format? I have searched and found a similar thread on SO:

Why does Amazon Advertising report API return a .bin instead of .json

I am also able to get a valid json file from postman using "Send and Download" option as suggested on this thread.

But stuck when trying to implement the unzip code in nodejs, the code snippet for parsing the response data

await axios.get(downloadurl, options)

        .then(async response => {
         console.log(response.data);
        
        // Calling gzip method
        zlib.gzip(response.data, (err, buffer) => {
  
        // Calling unzip method
        zlib.unzip(buffer, (err, buffer) => {
  
       console.log(buffer.toString('utf8'));
       finaldata =  buffer.toString('utf8');
      
     });
 });

// write data to file
 await fsPromises.writeFile('/Users/asbdb/Downloads/test1/test.json', finaldata);
    
        })
        .catch(error => {
            console.log({ error });
        });

Any pointers on how to unzip and convert the data in string format will be very helpful.

The above code snippet writes this to the output file: output in file


Solution

  • Use zlib.gunzip() to extract the contents.

    What you have there is a gzip stream, not a zip file, so zlib.unzip() won't help. zlib.gzip() compresses, as opposed to extracts, You are looking for extraction.