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

AWS Textract异步PDF处理调用DeleteMessageCommand遇InvalidAddress错误排查

问题分析:AWS Textract异步处理中DeleteMessageCommand报错InvalidAddress

错误信息

error InvalidAddress: The address arn:aws:sns:us-west-1:**** is not valid for this endpoint.

错误发生在processDocumentWithTextract函数的else分支(日志显示传入的sqsQueueUrl参数格式正确),相关代码如下:

async function processDocumentWithTextract(
  documentName: string,
  sqsQueueUrl: string,
  snsTopicArn: string
) {
  let jobFound = false
  let succeeded = false
  let response

  response = await textract.send(
    new StartDocumentAnalysisCommand({
      DocumentLocation: {
        S3Object: { Bucket: env.BUCKET_NAME, Name: documentName },
      },
      NotificationChannel: { RoleArn: env.ROLE_ARN, SNSTopicArn: snsTopicArn },
      FeatureTypes: ["TABLES"],
    })
  )

  const startJobId = response.JobId
  console.log(`Started job with ID: ${startJobId}`)

  // Poll SQS queue for job completion message
  while (!jobFound) {
    const sqsReceivedResponse = await sqsClient.send(
      new ReceiveMessageCommand({
        QueueUrl: sqsQueueUrl,
        MaxNumberOfMessages: 10,
      })
    )

    console.log("sqsReceivedResponse", sqsReceivedResponse)

    if (
      sqsReceivedResponse.Messages &&
      sqsReceivedResponse.Messages.length > 0
    ) {
      for (const message of sqsReceivedResponse.Messages) {
        const notification = JSON.parse(message.Body as string)
        const textractMessage = JSON.parse(notification.Message)

        if (textractMessage.JobId === startJobId) {
          console.log("Matching job found:", textractMessage.JobId)
          jobFound = true
          succeeded = textractMessage.Status === "SUCCEEDED"
          if (succeeded) {
            console.log("Job processing succeeded.")
            console.log("queueUrl", sqsQueueUrl)
            await sqsClient.send(
              new DeleteMessageCommand({
                QueueUrl: sqsQueueUrl,
                ReceiptHandle: message.ReceiptHandle,
              })
            )
            break
          }
        } else {
          await sqsClient.send(  // ---------------> error here, sqsQueueUrl looks correct when logging
            new DeleteMessageCommand({
              QueueUrl: sqsQueueUrl,
              ReceiptHandle: message.ReceiptHandle,
            })
          )
        }
      }
    }

    if (!jobFound) {
      // Sleep for a short while before polling the queue again
      console.log("polling queue...")
      await new Promise((resolve) => setTimeout(resolve, 5000))
    }
  }

  if (succeeded) {
    // Retrieve the results from Textract
    return startJobId
  } else {
    throw new Error("Textract job did not complete successfully.")
  }
}

错误原因分析

  1. SQS客户端区域与目标队列区域不匹配
    这是最常见的原因:错误提示里的SNS ARN属于us-west-1区域,但你的sqsClient可能配置了其他区域。SQS是区域性服务,跨区域发送DeleteMessage请求会被AWS拒绝,SDK会返回带有误导性的"SNS地址无效"错误(实际是队列区域与客户端区域不匹配导致的请求失败)。

  2. ReceiptHandle所属队列与当前请求队列不一致
    else分支中要删除的消息可能来自其他队列(比如之前轮询逻辑残留的跨队列消息),或者该ReceiptHandle已经过期、被删除过,导致与传入的sqsQueueUrl不匹配。

  3. SNS主题与SQS队列区域不兼容
    如果SNS主题在us-west-1,而SQS队列在另一个区域,两者的订阅关系可能存在隐性问题,导致消息的ReceiptHandle关联异常,最终触发删除操作时的地址有效性错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 08:10:24