0

trying to upload files using "firebase-admin" and "express-fileupload" , all firebase and express-fileupload configurations are setup as well :

my requires libs:

const firebaseAdmin = require("firebase-admin");
const fileUpload = require('express-fileupload');

my express-fileupload configs:

app.use(fileUpload({ createParentPath: true }))
app.use('/resources', express.static(path.resolve(__dirname, '../uploads')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); 

i tested express-fileuploa and it works fine:

debug for req.files.file:

{
  name: '66032925_2210721569219339_1152747532661555200_n.jpg',
  data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff ed 00 9c 50 68 6f 74 6f 73 68 6f 70 20 33 2e 30 00 38 42 49 4d 04 04 00 00 00 00 00 80 ... 23648 more bytes>,
  size: 23698,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'image/jpeg',
  md5: '41145e65bb895e594e4657c9f6ce90a3',
  mv: [Function: mv]
}

ok , now lets see my firebase configs:

var serviceAccount = require("../serviceAccountKey.json");

firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert(serviceAccount),
  storageBucket: "*****-*****.appspot.com"
});

the code bellow is the post callback upload endpoint , there i am working only to upload a myfile .

app.post("/upload", (req, res) => {
  try {

    var file = req.files.file;
    console.debug(file)

    var bucket = firebaseAdmin.storage().bucket();
    bucket.upload(file.data, { destination: "clients/avatars" }).then(data => {
      console.debug("data", data);
      return res.json({ message: "data", data: data });
    }, error => {
      console.debug("really ???", error)
      return res.json({ message: "error", data: error + "" });
    })

  } catch (err) {
    return res.json({ message: "error", data: err + "" });
  }
});

now , when i test it , i get this error :

"TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff ed 00 9c 50 68 6f 74 6f 73 68 6f 70 20 33 2e 30 00 38 42 ..."

i debug console i see the error is throwing from ***

bucket.upload(file.data, { destination: "clients/avatars" })

*** method call.any help with ?

1 Answers1

0

the reason for this is that you are passing the file data (as buffer) to a function that expects the file path.

bucket.upload(filePath, {
            destination: remoteFile,
            uploadType: "media",
            metadata: {
              contentType: fileMime,
              metadata: {
                firebaseStorageDownloadTokens: uuid
              }
            }
          })

refer complete code here: Upload files to Firebase Storage using Node.js

Yash Kumar Verma
  • 9,427
  • 2
  • 17
  • 28