如何在AWS Lambda函数中从form-Data中读取文件。

huangapple go评论73阅读模式
英文:

how to read a file from form-Data in an AWS Lambda function

问题

以下是翻译好的部分:

我正在尝试将PDF或文本文件发送到AWS Lambda函数(Node JS)。我希望能够在Lambda函数中处理这个文件。我知道通常最佳做法是使用来自S3存储桶的触发函数。但是,我想要能够从formdata发送这个文件到Lambda函数,从文件中提取信息,然后返回提取出的信息。

我已经能够首先将文件编码为64位二进制数据,并通过JSON将其发送到AWS Lambda,但通常当我尝试在Lambda函数中解码文件(特别是PDF文件)时,文件会损坏或为空。

图像文件似乎很适合这种类型的编码,但对于PDF文件一直没有成功。非常感谢任何帮助。除了使用base64进行编码之外,是否有一种方法可以从formdata获取文件?我的代码如下:

export const handler = async(event) => {
   
    console.log("event", event)
    
    var converted = atob(event.body) // 相反,我应该如何从formdata中读取文件



     const response =  {
        "statusCode": 200,
        "headers": {"Content-Type":"text/html", // "application/pdf", // "multipart/form-data", //
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"
                        
        },
        "body": event,
        "isBase64Encoded": true
        }
        
    return response;
};

非常感谢!

英文:

I am trying to send a PDF or text file to an AWS Lambda function (Node JS). I'd like to be able to process this file in the lambda function. I know often the best practice is to use a trigger function from an s3 bucket. However, I'd like to be able to send this lambda function a file from formdata, extract information from the file and then return the extracted info back.

I have been able to first encode the file in 64 bit binary data and send it to AWS lambda via a JSON, but often when I try to decode the file (especially a PDF) in the Lambda Function it is corrupted or empty.

Image files seem to work well for this type of encoding, but been unsuccessful with PDFs. Any help greatly appreciated. Rather then encode in base 64 is there a way I can obtain the file from a formdata? My code is below:

export const handler = async(event) => {
   
    console.log("event", event)
    
    var converted = atob(event.body) // RATHER HOW WOULD I READ A FILE FROM FORMDATA



     const response =  {
        "statusCode": 200,
        "headers": {"Content-Type":"text/html", //"application/pdf", // "multipart/form-data", //
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"
                        
        },
        "body": event,
        "isBase64Encoded": true
        }
        
    return response;
};

thanks so much

答案1

得分: 3

我假设您正在使用API网关来触发Lambda函数。在这种情况下,您需要按照文档中提到的步骤启用API网关中的multipart/form-data。文档的要点如下:

  1. 设置窗格中,在二进制媒体类型部分选择添加二进制媒体类型。在输入文本字段中键入所需的媒体类型,例如image/png
  2. 在代理方法的请求标头中添加Content-TypeAccept
  3. 将这些相同的标头添加到集成请求标头中。
  4. 部署API

附注:如果您正在使用Lambda代理集成({proxy+}),只需要完成步骤1和步骤4。

英文:

I assume that you are using an API gateway to trigger the lambda function. In that case, you have to enable multipart/form-data in API gateway as mentioned in the documentation. The gist of the documentation is as follows:

  1. In the Settings pane, choose Add Binary Media Type in the
    Binary Media Types section. Type a required media type, for example, image/png, in the input text field.
  2. Add Content-Type and Accept to the request headers for your proxy method.
  3. Add those same headers to the integration request headers.
  4. Deploy the API

PS: If you are using Lambda Proxy integration ({proxy+}, just steps 1 and 4 are enough.

huangapple
  • 本文由 发表于 2023年1月5日 07:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75012304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定