1

上传到firebase云功能时遇到问题..我正在使用busboy和sharp来实现这一点,但每次我运行我的代码时它只返回“完成状态:'超时'”没有错误......

此代码将在没有急剧调整大小功能的情况下工作,请参阅下面的代码以供参考。

const fs = require("fs");
const os = require("os");
const path = require("path");
const Busboy = require("busboy");
const config = require('../config');
const gcs = config.get('gcs');
const UUID = require('uuidv4')
const sharp = require('sharp');


 let insertStorageMaterials = (req,callback) =>{
  const busboy = new Busboy({ headers: req.headers });
  let uploadData = null;
  let origFileName;

  busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
    origFileName = filename
    const filepath = path.join(os.tmpdir(), filename);
    uploadData = { file: filepath, type: mimetype };
    file.pipe(fs.createWriteStream(filepath));

  });

  busboy.on("finish", () => {
    const bucket = gcs.bucket(bucketName);
    let uuid = UUID();

    sharp(uploadData.file)
    .resize(500, 500)
    .toFile(uploadData.file)
    .then(data => {
      console.log("data",data)
      bucket
      .upload(uploadData.file, {
        uploadType: "media",
        metadata: {
          metadata: {
            contentType: uploadData.type,
            firebaseStorageDownloadTokens: uuid
          }
        },
        destination:"Materials/test/"+origFileName  
    })
    return data
  }).then(data=>{
      let file = data[0];
      let dataURL = "https://firebasestorage.googleapis.com/v0/b/" + bucket.name + "/o/" + encodeURIComponent(file.name) + "?alt=media&token=" + uuid;
      console.log("url",dataURL)
      return callback(false,{dataURL:dataURL,name:origFileName,message:"Success!"});
    .catch(error =>{
      return error;
    })
  })

  busboy.end(req.rawBody);

  }

module.exports = {
  insertStorageMaterials:insertStorageMaterials
}

特快电话

//index.js
const app = express();
const appUsers = require('./Routes/userRoute')

const api = fbFunc.https.onRequest(app);

app.post(['/uploadMaterials'],appUsers);


module.exports = {api}

//Routes
const userFunc = require('../Functions/userFunc')

apiCalls.post('/uploadMaterials',(req,res)=>{
    userFunc.uploadMaterials(req,(err,result)=>{
      if (err) return res.status(500).send('Internal Server Error')
      return res.status(200).send(result);
  })
})

编辑:

  sharp(uploadData.file)
    .resize(500, 500)
    .toFile(uploadData.file)
    .then(data => {
      console.log("datadddd",data)
      return bucket
      .upload(uploadData.file, {
        uploadType: "media",
        metadata: {
          metadata: {
            contentType: uploadData.type,
            firebaseStorageDownloadTokens: uuid
          }
        },
        destination:"Materials/test/"+origFileName  
    })

  .then(data=>{
      let file = data[0];
      let dataURL = "https://firebasestorage.googleapis.com/v0/b/" + bucket.name + "/o/" + encodeURIComponent(file.name) + "?alt=media&token=" + uuid;
      console.log("url",dataURL)
      return callback(false,{dataURL:dataURL,name:origFileName,message:"Success!"});
    })
    .catch(error =>{
      return error;
    })

}).catch(error =>{
  return error;
})
  })
4

2 回答 2

0

当您将响应发送回客户端时,HTTP 函数会终止。由于您从不发送响应,因此该函数将超时等待该响应。使用resExpress 传递给您的代码的那个对象。

于 2018-12-17T07:46:06.640 回答
0

不确定问题是否与调整大小 API 有关,但您是否尝试使用 toBuffer 方法而不是 toFile?

sharp(input)
    .resize(500, 500)
    .toBuffer()
    .then(data => {
    // Convert data to image file
    });

您可以使用此处接受的答案将图像缓冲区转换为文件 - NodeJS write base64 image-file

于 2018-12-17T06:25:38.407 回答