Headers getting added to file content while retrieving file from APIGatewayProxyRequestEvent in AWS lambda

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

Headers getting added to file content while retrieving file from APIGatewayProxyRequestEvent in AWS lambda

问题

我正在使用AWS Lambda通过Java代码将文件推送到S3。当我从Postman或Angular发送文件时,我尝试在Java函数中打印文件的内容。在这样做的过程中,文件内容会自动添加头,类似于:

"----------------------------965855468995803568737630
Content-Disposition: form-data; name="test"; filename="test.pdf"
Content-Type: application/pdf"
.

如何从APIGatewayProxyRequestEvent中获取不带头部的文件内容?

这是我用来打印文件内容的代码:

context.getLogger().log("Input File: " + apiGatewayProxyRequestEvent.getBody());
英文:

I am using AWS Lambda to push the file to S3 through Java code.
While sending the file from Postman or from Angular I am trying to print the content of file in Java functions. While doing so headers are getting added to the file content automatically like:

"----------------------------965855468995803568737630
Content-Disposition: form-data; name="test"; filename="test.pdf"
Content-Type: application/pdf"

.

How to get the file content without headers from APIGatewayProxyRequestEvent?.

This is code am using to print the file content.

context.getLogger().log("Input File: "+apiGatewayProxyRequestEvent.getBody());

答案1

得分: 2

这对你来说是一个棘手的问题。方法 getBody() 将会给你实际发送到 APIGatewayProxyRequest 的请求主体,因此它会返回发送的内容,这些内容是以 form-data 形式编码的,带有 Content-Type 和文件名。如果你想要打印内容,责任在于你将 form-data 转换回可理解的对象格式。

如果你查看一下这个Medium 上的教程,你可以看到一种处理方法。它归结为处理数据并处理格式边界:

// 获取上传的文件并从 base64 解码
byte[] bI = Base64.decodeBase64(event.getBody().getBytes());
// 获取 content-type 标头并提取边界
Map<String, String> hps = event.getHeaders();
if (hps != null) {
    contentType = hps.get("content-type");
}
String[] boundaryArray = contentType.split("=");
// 将边界转换为字节数组
byte[] boundary = boundaryArray[1].getBytes();
// 记录提取以进行验证
logger.log(new String(bI, "UTF-8") + "\n");

最后一行将会得到你想要的,即打印主体内容,当然,如果它是二进制格式,对你可能不是很有用。我建议你彻底阅读一下那篇教程,因为它会帮助你了解如何迭代数据流并创建对象。

英文:

This is a tricky one for you to solve. The method getBody() will give you the actual request body that is sent through the APIGatewayProxyRequest so it's going to give you back what is sent through, which is the file encoded as form-data with a Content-Type and a filename. The responsibility lies on you to convert the form-data back into an understandable object format if you wan to print the content.

If you have a look at this tutorial on Medium you can see an approach to this. It boils down to processing the data and working with the format boundary:

//Get the uploaded file and decode from base64 
      byte[] bI = Base64.decodeBase64(event.getBody().getBytes()); 
      //Get the content-type header and extract the boundary 
      Map&lt;String, String&gt; hps = event.getHeaders(); 
      if (hps != null) { 
        contentType = hps.get(&quot;content-type&quot;); 
      } 
      String[] boundaryArray = contentType.split(&quot;=&quot;); 
      //Transform the boundary to a byte array 
      byte[] boundary = boundaryArray[1].getBytes(); 
      //Log the extraction for verification purposes 
      logger.log(new String(bI, &quot;UTF-8&quot;) + &quot;\n&quot;); 

That last line will get you what you want, which is printing the body content, obviously if it's a binary format that might not be very useful for you. I'd recommend giving that tutorial a full read as it will help show you how to iterate through the data stream and create the object.

huangapple
  • 本文由 发表于 2020年9月9日 18:09:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63809394.html
匿名

发表评论

匿名网友

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

确定