我的代码在本地运行时工作正常,在部署之前我已经用npm install chrome-aws-lambda
安装了模块,并尝试了chmod -R +r node_modules/
,以防出现读取权限错误,但还是没有运气。
我的handler.js代码:
'use strict';
const {getArrayOfPages} = require("./utils");
const creds = require("./creds.json");
module.exports.hello = async (event, context) => {
try {
var browserendpoint = await getArrayOfPages(creds.username, creds.password)
console.log("DATA: " + browserendpoint)
}
catch(error) {
// Code that handles any potential errors
console.log(error)
}
}
我的getArrayOfPages.js代码:
const sign_in = 'www.example.com/';
const chromium = require('chrome-aws-lambda')
module.exports = async function (username, password) {
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
slowMo: 250, // slow down by 250ms
userDataDir: "/tmp/" //save session data/cookies to be shared between lambda functions
});
const endpoint = await browser.wsEndpoint();
const page = await browser.newPage();
await page.goto(sign_in, { waitUntil: 'networkidle0' })
// code to login here
await browser.disconnect()
return endpoint
}
catch (error) {
// Code that handles any potential errors
console.log(error)
}
}