Firebase云函数中运行Puppeteer时出现问题

lh80um4z  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(130)

我似乎不能得到Puppeteer正常工作时,部署到Firebase云功能。

人偶师:我不能上传我的项目时使用Puppeteer,因为我上传的Chrome的Puppeteer版本在我的“functions”文件夹中,然后它超过了允许的大小限制。这是一条合适的路吗?我可以绕过大小限制吗?
傀儡核心:我找不到合适的Chrome浏览器来使用它。“Chrome-aws-lambda”似乎不适用于最新版本的Puppeteer-core。也许这里有人知道一个更新的替代品?

或者有一个我不知道的解决方案?
我试着给我的函数分配更多的内存(从256 MB增加到1 GB),但没有帮助。我还尝试在Puppeteer中添加“无沙盒”标志,但没有帮助。我已经确保Puppeteer下载的Chrome版本位于我的“functions”目录中。这个版本的Chrome似乎太大了,无法上传到Firebase Cloud Functions。我还尝试在代码中添加Chrome的新无头模式,但没有成功。
这是我当前的Puppeteer浏览器代码:

browser = await puppeteer.launch({ 
            headless: "new",
            args: ['--no-sandbox', '--disable-setuid-sandbox']
        });

这是我将内存增加到1GB的代码:

exports.scheduledFunction = functions
    .runWith({ memory: '1GB' })

谢谢!

nbysray5

nbysray51#

我建议降低你的 puppet 版本。一些较新的版本在与Cloud Functions一起使用时会出现问题。您不需要在功能文件夹中包含Chrome。
我在4GB内存的云函数中使用Puppeteer 16.2.0,它对我来说工作得很好。这是我的代码:

async function get_browser() {
  const browser = await puppeteer.launch({
      args: ["--no-sandbox", "--disable-setuid-sandbox"]
  });

  return browser;
}

调用get_browser的父函数初始化为:

exports.functionName = functions.runWith({memory: "4GB", timeoutSeconds: 540}).https.onRequest(async (request, response) => {

(Long超时时间是因为函数执行多个操作,我很谨慎)

相关问题