You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js AWS Lambda调用Adobe PDF Services报错:No input was set for operation

解决Adobe PDF Services API报错“No input was set for operation”

这个报错核心是你没有给Adobe的导出操作正确传入PDF输入文件,大概率是S3文件读取环节出了问题,或是没把读取到的内容正确传递给Adobe API。以下是具体排查和修正步骤:

1. 确保正确读取S3中的PDF文件

Lambda里读取S3文件必须拿到完整的文件Buffer,且要处理好异步操作。别用回调式的getObject,改用@aws-sdk/client-s3的Promise版本配合async/await:

const s3Client = new S3Client({ region: "你的区域" });
const getObjectCmd = new GetObjectCommand({
  Bucket: "你的存储桶名",
  Key: "PDF文件的S3键"
});
const s3Response = await s3Client.send(getObjectCmd);
// 将S3的Stream转换为Buffer
const pdfBuffer = await streamToBuffer(s3Response.Body);

// 辅助函数:Stream转Buffer
function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream.on('data', chunk => chunks.push(chunk));
    stream.on('end', () => resolve(Buffer.concat(chunks)));
    stream.on('error', reject);
  });
}

添加日志打印pdfBuffer.length,确认是否读取到非空的文件内容。如果长度为0,说明S3读取失败,检查存储桶权限、文件键是否正确。

2. 正确配置Adobe PDF Services的输入

用Adobe SDK的FileRef.createFromBuffer把读取到的Buffer转成API可接受的输入对象,再传入ExportPDFJob:

// 初始化Adobe凭证
const credentials = new ServicePrincipalCredentials({
  clientId: process.env.ADOBE_CLIENT_ID,
  clientSecret: process.env.ADOBE_CLIENT_SECRET
});

const executionContext = new ExecutionContext(credentials);
const pdfServices = new PDFServices(executionContext);

// 创建输入文件引用
const inputFileRef = FileRef.createFromBuffer(pdfBuffer, "input.pdf");

// 创建导出作业
exportPDFJob = new ExportPDFJob({
  input: inputFileRef,
  targetFormat: ExportPDFTargetFormat.DOCX
});

注意:

  • 必须明确传入input参数,且不能是undefined或空值
  • 确保ExportPDFTargetFormat.DOCX拼写正确(SDK枚举值要对应)

3. 完整的Lambda Handler示例

const { S3Client, GetObjectCommand, PutObjectCommand } = require("@aws-sdk/client-s3");
const { ExecutionContext, ServicePrincipalCredentials, PDFServices, ExportPDFJob, ExportPDFTargetFormat, FileRef } = require("@adobe/pdfservices-node-sdk");

const s3Client = new S3Client({ region: "us-east-1" }); // 替换为你的区域

async function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream.on('data', chunk => chunks.push(chunk));
    stream.on('end', () => resolve(Buffer.concat(chunks)));
    stream.on('error', reject);
  });
}

exports.handler = async (event) => {
  try {
    // 1. 从S3读取PDF文件
    const getObjectCmd = new GetObjectCommand({
      Bucket: "your-bucket-name",
      Key: "path/to/input.pdf" // 替换为你的文件键
    });
    const s3Response = await s3Client.send(getObjectCmd);
    const pdfBuffer = await streamToBuffer(s3Response.Body);
    console.log(`读取到PDF文件,大小:${pdfBuffer.length}字节`);

    // 2. 初始化Adobe PDF Services
    const credentials = new ServicePrincipalCredentials({
      clientId: process.env.ADOBE_CLIENT_ID,
      clientSecret: process.env.ADOBE_CLIENT_SECRET
    });
    const executionContext = new ExecutionContext(credentials);
    const pdfServices = new PDFServices(executionContext);

    // 3. 创建导出作业并执行
    const inputFileRef = FileRef.createFromBuffer(pdfBuffer, "input.pdf");
    const exportPDFJob = new ExportPDFJob({
      input: inputFileRef,
      targetFormat: ExportPDFTargetFormat.DOCX
    });

    const jobResult = await pdfServices.submit(exportPDFJob);
    const resultStream = await jobResult.getResult().getStream();
    const docxBuffer = await streamToBuffer(resultStream);

    // 4. 将转换后的DOCX上传回S3
    const putObjectCmd = new PutObjectCommand({
      Bucket: "your-bucket-name",
      Key: "path/to/output.docx", // 替换为输出路径
      Body: docxBuffer,
      ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    });
    await s3Client.send(putObjectCmd);

    return { statusCode: 200, body: "转换完成" };
  } catch (error) {
    console.error("转换失败:", error);
    throw error;
  }
};

额外排查点

  • 检查Lambda执行角色是否有S3的GetObject和PutObject权限
  • 确认Adobe的凭证(Client ID/Secret)已正确配置为Lambda环境变量,且有权限调用Export PDF API
  • 确保使用的@adobe/pdfservices-node-sdk是最新稳定版,避免旧版本API兼容问题

内容的提问来源于stack exchange,提问作者rahul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:10:33