上传到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;
})
})