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

1

5

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);
    }
  });
}
Metaphor answered 5/8, 2016 at 12:53 Comment(0)
M
13

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);
    }
  });
}
Metaphor answered 5/8, 2016 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.