Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the Content-Type for a file being uploaded to Google Cloud Storage using node.js

I'm writing code that uploads a file that was fetched using node-fetch to a bucket in Google Cloud Storage using gcloud-node.

So far everything works out, but I'm having trouble finding out how to set the Content-Type for the file on GCloud, otherwise the file is downloaded as application/octet-stream.

This is the code I have so far:

function saveImage(response, destFile) {
  return new Promise((resolve, reject) => {
    if (response.status !== 200) {
      reject(new Error('Bad Response'));
    }

    // How do I set this on the file uploaded to the bucket?
    const contentType = response.headers.get('Content-Type');

    try {
      const file = bucket.file(destFile);
      const writeStream = file.createWriteStream();

      writeStream.on('error', err => {
        reject(err);
      });

      writeStream.on('finish', () => {
        resolve(getPublicUrl(destFile));
      });

      response.body.pipe(writeStream);
    } catch (e) {
      reject(e);
    }
  });
}
like image 713
andreban Avatar asked Dec 15 '22 04:12

andreban


1 Answers

It turns out there's a setMetadata method on file, which you can call passing the Content-Type as a parameter. Here's what the working code looks like:

function saveImage(response, destFile) {
  return new Promise((resolve, reject) => {
    if (response.status !== 200) {
      reject(new Error('Bad Response'));
    }

    const contentType = response.headers.get('Content-Type');
    try {
      const file = bucket.file(destFile);

      const writeStream = file.createWriteStream({
        metadata:{
          contentType: contentType
        }
      });

      writeStream.on('error', err => {
        reject(err);
      });

      writeStream.on('finish', () => {
        resolve(getPublicUrl(destFile));
      });

      response.body.pipe(writeStream);
    } catch (e) {
      reject(e);
    }
  });
}
like image 69
andreban Avatar answered Dec 16 '22 17:12

andreban