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