在firebase cloud函数/Nodejs中使用busboy和sharp调整图像大小



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

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新