如何使用Node.js和Postman将文件上传到Firebase?

时间:2019-08-19 13:05:40

标签: node.js firebase google-cloud-storage postman

我有一个文件nodejs,它将图片(lion.jpg)添加到Cloud Storage

const firebase = require('firebase-admin');
const express = require('express');
const app = express();
const serviceAccount= require("./key9525")  

firebase.initializeApp({
  credential: firebase.credential.cert(serviceAccount),
  databaseURL: "https://myadress.firebaseio.com"  //example adress
});

const bucketName = 'myadress.appspot.com';
const filename ='./lion.jpg';     //example file

async function uploadFile() {

  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage();

  await storage.bucket(bucketName).upload(filename, {

    gzip: true,
    metadata: {
      cacheControl: 'public, max-age=31536000',
    },
  });
  console.log(`${filename} uploaded to ${bucketName}.`);
}

uploadFile();

我有一个文件,可以选择和上传照片,例如邮递员

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('hello people');
});

app.listen(port, () => {
    console.log('listening to the port: ' + port);
});

var multer = require('multer');
var upload = multer({dest:'uploads/'});

app.post('/single', upload.single('profile'), (req, res) => {
  try {
    res.send(req.file);
  }catch(err) {
    res.send(400);
  }
}

我如何连接这些代码,以便在加载nodejs文件之后,在邮递员中选择文件,然后将该文件上传到firebase中? 感谢所有提示

1 个答案:

答案 0 :(得分:0)

您必须像执行操作一样将multer作为中间件传递给POST调用。请参阅此link