puppeteer官方Docker镜像如何使用?

puppeteer官方Docker镜像:https://pptr.nodejs.cn/guides/docker

现在我的需求是使用express创建接口,当调用接口传递链接,再puppeteer访问链接进行截图,截图后把截图上传阿里Oss,返回上传阿里Oss后的图片链接
我的想法是再打包一次:
image.png

FROM ghcr.io/puppeteer/puppeteer

把官方镜像打包进行我们自己的Dockerfile,请大佬们这样可行吗?这个Dockerfile配置应该怎么写?请大佬指点下

阅读 4.2k
1 个回答

Dockerfile

FROM ghcr.io/puppeteer/puppeteer:latest

RUN npm install express ali-oss
COPY app.js .
CMD node app.js

app.js

const express = require('express');
const puppeteer = require('puppeteer');
const OSS = require('ali-oss');

const app = express();
const port = 3000;

app.use(express.json());

// Replace with your own OSS configuration
const ossConfig = {
  accessKeyId: 'xxxx',
  accessKeySecret: 'xxxx',
  bucket: 'zzzzz',
  region: 'oss-cn-hangzhou'
};

const ossClient = new OSS(ossConfig);

app.get('/screenshot', async (req, res) => {
  const { url } = req.query;

  if (!url) {
    return res.status(400).json({ error: 'Missing "url" parameter.' });
  }

  try {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto(url);
    const screenshot = await page.screenshot();

    await browser.close();

    // Generate a unique filename for the screenshot
    const filename = `screenshot-${Date.now()}.png`;
    console.log(filename)
    // Upload the screenshot to OSS
    const result = await ossClient.put(filename, screenshot);

    // Construct the OSS URL for the uploaded image
    const imageUrl = result.url;

    res.json({ imageUrl });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'An error occurred while taking the screenshot and uploading it to OSS.' });
  }
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题