user3642173 user3642173 - 4 days ago 5
Node.js Question

Attributes for image stored using Google Cloud Storage

I am using the NPM documentation for uploading an image to Google Cloud Storage

app.post('/test', upload.single('image'), function (req, res, next) {
});
let bucket = gcs.bucket('bucket-name');

bucket.upload(req.file.path, function(err, file) {
if (err) {
throw new Error(err);
}
console.log(file);
});

console.log(req.file);

// req.body will hold the text fields, if there were any
})


Is there any way I can specify a custom name and file type for the file I'm uploading? In my case it should be an image.

Answer

You can pass options to your upload method:

var options = {
  destination: 'my-image-name.jpg',
  metadata: {
    contentType: 'image/jpeg',
  }
};

bucket.upload(req.file.path, options, function(err, file) {
});
Comments