node.jsgoogle-cloud-storagegcloud-node

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);
    }
  });
}

Solution

  • 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);
        }
      });
    }