首页 > 解决方案 > 错误:在 Firebase 文件存储中找不到

问题描述

我正在尝试使用 node.js 将文档文件从本地上传到 firebase 文件存储。

但我收到如下错误:

错误:未找到

下面是我的代码。你能请任何人帮助我吗?

const keyFilename="./xxxxxxxx.json"; 
const multer = Multer({
    fileFilter: function(req, file, cb){
        checkFiletype(file, cb)
    },
  storage: Multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024 // no larger than 5mb, you can change as needed.
  }
});

const storage = googleStorage({
  projectId: 'xxxx' ,
  keyFilename :keyFilename
});

const bucket = storage.bucket("gs://xxxx/xxxx");


const uploadImageToStorage = (file) => {
  let prom = new Promise((resolve, reject) => {
    if (!file) {
      reject('No image file');
    }
    console.log('filw',file);
    let newFileName = `${file.originalname}_${Date.now()}`;

    let fileUpload = bucket.file(newFileName);

    const blobStream = fileUpload.createWriteStream({
      metadata: {
        contentType: file.mimetype
      }
    });

    blobStream.on('error', (error) => {
        console.log(error);
      reject('Something is wrong! Unable to upload at the moment.');
    });

    blobStream.on('finish', () => {
      // The public URL can be used to directly access the file via HTTP.
      const url = util.format(`https://storage.googleapis.com/${bucket.name}/${fileUpload.name}`);
      resolve(url);
    });

    blobStream.end(file.buffer);
  });
  return prom;
} 

错误:以下是我将文件上传到 Firebase 文件存储时遇到的错误。

Error: Not Found
    at Request._callback (C:\Users\rkanumetta\Desktop\uploadapi\node_modules\gcs
-resumable-upload\build\src\index.js:248:33)
    at Request.self.callback (C:\Users\rkanumetta\Desktop\uploadapi\node_modules
\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\Users\rkanumetta\Desktop\uploadapi\node_modules\r
equest\request.js:1157:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (C:\Users\rkanumetta\Desktop\uploadapi\node_m
odules\request\request.js:1079:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
Something is wrong! Unable to upload at the moment.

标签: node.jsfirebasefirebase-storage

解决方案


我希望它对你有用。我从本地上传了一个文件,然后我使用 UUID 添加了访问令牌,然后我上传到了 firebase 存储。在我生成下载 url 之后。如果我们点击生成 url,它将自动下载一个文件。

 const keyFilename="./xxxxx.json"; //replace this with api key file
    const projectId = "xxx" //replace with your project id
    const bucketName = "xx.xx.appspot.com"; //Add your bucket name
    var mime=require('mime-types');
    const uuidv1 = require('uuid/v1');//this for unique id generation

    //const mime = require('mime');
    const gcs = require('@google-cloud/storage')({
        projectId,
        keyFilename
    });

    const bucket = gcs.bucket(bucketName);

    const filePath = "./sample.odp";
    const remotePath = "/XXXX";
    const fileMime = mime.lookup(filePath);

//we need to pass those parameters for this function
    var upload = (filePath, remoteFile, fileMime) => {

      let uuid = uuidv1();

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

              let file = data[0];

              return Promise.resolve("https://firebasestorage.googleapis.com/v0/b/" + bucket.name + "/o/" + encodeURIComponent(file.name) + "?alt=media&token=" + uuid);
          });
    }
//This function is for generation download url    
 upload(filePath, remotePath, fileMime).then( downloadURL => {
        console.log(downloadURL);

      });

推荐阅读