简体   繁体   中英

Cloud Functions for Firebase Endpoint to Process an Image

I want to create a cloud function endpoint that can accept and process image files such as JPEG. This is my code:

var express = require('express')
var multer  = require('multer')
//firebase complains that '/user_code/tmp' is read-only if I use this
//var upload = multer({ dest: 'tmp/' })
var upload = multer({
  storage: multer.MemoryStorage,
  fileSize: 5 * 1024 * 1024
});
var app = express()

app.post('/image-upload', upload.single('image'), function(req, res, next) {

   console.log("request data: " + JSON.stringify(req.data));
   console.log("request params: " + JSON.stringify(req.params));
   console.log("request query: " + JSON.stringify(req.query));
   console.log("request body: " + JSON.stringify(req.body));
   console.log("request headers: " + JSON.stringify(req.headers));
   console.log("req.file: " + req.file);
   console.log("req.files: " + req.files);

   res.status(201).send(`My Server Response is ${req.file}`);
});

module.exports.testEndpoint = functions.https.onRequest(app);

For some reason, req.file and req.files is always undefined . However, req.body does appear to contain my image. I am using Postman to send the HTTP POST request.

邮递员的输入和响应

I figured it out. My "Header" was incorrect. Make sure to keep the Content-Type field unchecked.

在邮递员中取消选中内容类型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM