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);
}
});
}
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);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With