AWS Lambda和S3-上传的pdf文件为空白/损坏 [英] AWS Lambda and S3 - uploaded pdf file is blank/corrupt

查看:230
本文介绍了AWS Lambda和S3-上传的pdf文件为空白/损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆