我具有图像调整大小云功能。当我回顾日志时,我看到函数执行花费了1014毫秒。但是执行后,该功能仍会显示日志(正在执行应做的操作,图像会调整大小)。
那么日志上会有延迟吗?或者执行该功能真的需要一分钟以上的时间(这对帐单有什么问题吗?)?
谢谢!
更新:这是我的代码:
export const makeRoundPicture = functions.https.onCall(async (data, context) => {
var bucket = admin.storage().bucket();
console.log(context.auth.uid);
if (context.auth.uid != null) {
await tmp.dir(async function _tempDirCreated(err: any, path: any) {
if (err) throw err;
var initialurl = "https://firebasestorage.googleapis.com/v0/b/myproject.appspot.com/o/"+context.auth.uid+".jpg?alt=media";
const options = {
url: initialurl,
dest: path
}
console.log('Dir: ', path);
await download.image(options)
.then(async (filename: any) => {
console.log('Saved');
console.log(filename['filename']);
var uploadPath = filename['filename'];
var original = uploadPath;
var output = path+"photo.png";
var size = 230;
await gm(original)
.resize(size, size)
.write(output, function() {
gm(size, size, 'none')
.fill(output)
.drawCircle(size/2.05 ,size/2.05, size/2.05, 0)
.write(output, async function(err6: any) {
console.log(err6 || 'done');
await bucket.upload(output, {
destination: "myfolder/"+context.auth.uid+".png",
});
});
});
})
.catch((err4: any) => console.error(err4))
});
} else {
console.log("unAuthorized request");
}
});
答案 0 :(得分:1)
您的问题来自于Promises和回调API的混合。
async
/ await
语法仅在将诺言链接在一起时起作用。
作为示例,此await tmp.dir
行不等待_tempDirCreated
进行评估并返回其等待的诺言。
await tmp.dir(async function _tempDirCreated(err: any, path: any) {
/* ... */
});
// this line gets called immediately
因此,简而言之,您将需要采用基于承诺的API,例如在tmp-promise
处使用node-tmp
,或将整个函数包装在Promise中。
const tmp = require('tmp-promise');
export const makeRoundPicture = functions.https.onCall(async (data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
console.log('Called by: ', context.auth.uid);
const bucket = admin.storage().bucket();
const { path: tmpDirPath } = await tmp.dir();
const initialurl = "https://firebasestorage.googleapis.com/v0/b/myproject.appspot.com/o/"+context.auth.uid+".jpg?alt=media";
const options = {
url: initialurl,
dest: tmpDirPath
}
console.log('Temp Dir: ', tmpDirPath);
let { filename: originalFilename } = await download.image(options);
console.log('Saved: ', originalFilename);
let output = tmpDirPath + "photo.png";
let size = 230;
await gmMakeRoundPicture(originalFilename, output, size);
await bucket.upload(output, {
destination: "myfolder/"+context.auth.uid+".png",
});
});
function gmMakeRoundPicture(inFilePath, outFilePath, size) {
return new Promise((resolve, reject) => {
gm(inFilePath)
.resize(size, size)
.write(outFilePath, async (err) => {
if (err) return reject(err);
gm(size, size, 'none')
.fill(outFilePath)
.drawCircle(size/2.05, size/2.05, size/2.05, 0)
.write(outFilePath, (err) => err ? reject(err) : resolve());
})
});
}