1

I need help in uploading the gzip file into gcs. I am using nest js and @google-cloud/storage package.

  @Post('/uploadFile')
  async uploadFile(@Req() req: Request, @Res() res: Response) {
    req.setEncoding('utf-8');
    req.on('data', (chunk) => {
      const storage = new Storage({ keyFilename: 'datastore.json' });
      const bucket = storage.bucket('bucket_name');
      let mimeType = req.headers.filetype;
      let file = req.headers.filename;
      const blob = bucket.file(file + '');
      let strOut = "";
      for (var i = 0; i < chunk.length; i++) {
          strOut += chunk[i].charCodeAt(0).toString(2);
      }
      blob.save(strOut, {
        metadata: {
          contentType: mimeType,
          contentEncoding: 'gzip'
        },
        resumable: false,
        gzip:true
      });
   });

I am facing an issue while downloading the data file got downloaded but shows damaged/corrupted may be i am uploading the .gz file wrong. I am streaming the .gz file like this

   fs.createReadStream("compressedData/" + names[i]).pipe(
        request.post(
          {
            headers: config.headers,
            url: "http://localhost:3000/uploadFile",
          },
          (err, res) => {
            if (err) throw err;
            console.log(res.body, config);
          }
        )
      );

here is my config from client side

   config = {
          headers: {
            filetype: "application/gzip",
            filename: "sitemap/test-upload/" + names[i],
            token: "sadjfbkjbkjvkjvkvkcvkjxsdnkn",
          },
        };
  • Are you setting `Accept-Encoding: gzip` on the download? I'm wondering if you're not doing that and thus getting the content served gunzipped, with the client side expecting it to be gzip encoded still. – Mike Schwartz Jul 06 '22 at 19:12
  • I am downloading from the console url... Not from specific code right now – Vedant Pruthi Jul 06 '22 at 23:56
  • Have you verified your gzip files in Cloud Storage meet [these two criteria](https://cloud.google.com/storage/docs/transcoding#decompressive_transcoding)? – Osvaldo Jul 07 '22 at 17:07
  • yes i have verified it – Vedant Pruthi Jul 12 '22 at 08:25
  • Have you tried with `contentType: text/plain` or other than `mimeType`? Take a look at [this post](https://stackoverflow.com/questions/48691415/content-type-vs-mime-type) and [this other](https://stackoverflow.com/questions/63994341/nestjs-accepting-multiple-mime-types-in-request-body). Also, take a look at [this example](https://docs.nestjs.com/techniques/streaming-files#example). – Osvaldo Jul 19 '22 at 19:52

0 Answers0