AWS Lambda和S3-上传的pdf文件为空白/损坏 [英] AWS Lambda and S3 - uploaded pdf file is blank/corrupt
问题描述
我有一个Spring App(在AWS Lambda上运行),该应用程序获取文件并将其上传到AWS S3.
I have an Spring App(running on AWS Lambda) which gets a file and uploads it on AWS S3.
Spring Controller将MultipartFile
发送到我的方法,并使用Amazon API Gateway将其上传到AWS S3.
The Spring Controller sends a MultipartFile
to my method, where it's uploaded to AWS S3, using Amazon API Gateway.
public static void uploadFile(MultipartFile mpFile, String fileName) throws IOException{
String dirPath = System.getProperty("java.io.tmpdir", "/tmp");
File file = new File(dirPath + "/" + fileName);
OutputStream ops = new FileOutputStream(file);
ops.write(mpFile.getBytes());
s3client.putObject("fakebucketname", fileName, file);
}
我尝试上传一个包含2页文本的PDF文件.上传后,PDF文件(在AWS S3上)有2个空白页.
I try to upload a PDF file which has 2 pages with text. After upload, the PDF file(on AWS S3) has 2 blank pages.
为什么上传的PDF文件为空白?
Why is the uploaded PDF file blank?
我还尝试了其他文件(例如PNG图像),当我打开它时,上传的图像已损坏.
I also tried with other files(like PNG image) and when I open it the image I uploaded is corrupted.
唯一有效的是我上载了文本文件.
The only thing that worked was when I uploaded a text file.
推荐答案
原来,这可以解决这个问题.感谢@KunLun的帮助,这一切都与编码有关.在我的情况下,文件是通过POST传递到url的aws组成的多部分文件(pdf).
Turns out that this will do this trick. Its all about encoding, thanks to the help of @KunLun. In my scenario, file is the multipart file (pdf) that is passed to aws via a POST to the url.
- 服务器获取具有此字节的文件-> 0010(这不会 解释正确,因为一个标准字节有8位)
- 所以,我们将其编码为base 64->不管结果如何
- 将其解码以获取标准字节-> 0000 0010(现在这是一个标准字节,并且由aws正确解释)
- 此来源也有很大帮助-> https://www.javaworld.com/article/3240006/base64-encoding-and-decoding-in-java-8.html?page=2
- server gets a file with this byte -> 0010 (this will not be interpreted right, because a standard byte has 8 bits)
- so, we encode it in base 64 -> doesn't matter what result
- decode it to get a standard byte -> 0000 0010 (now this is a standard byte and it's interpreted right by aws)
- This source here helped a lot as well --> https://www.javaworld.com/article/3240006/base64-encoding-and-decoding-in-java-8.html?page=2
Base64.Encoder enc = Base64.getEncoder();
byte[] encbytes = enc.encode(file.getBytes());
for (int i = 0; i < encbytes.length; i++)
{
System.out.printf("%c", (char) encbytes[i]);
if (i != 0 && i % 4 == 0)
System.out.print(' ');
}
Base64.Decoder dec = Base64.getDecoder();
byte[] barray2 = dec.decode(encbytes);
InputStream fis = new ByteArrayInputStream(barray2);
PutObjectResult objectResult = s3client.putObject("xxx", file.getOriginalFilename(), fis, data);
这篇关于AWS Lambda和S3-上传的pdf文件为空白/损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!