AWS Lambda and S3 - uploaded pdf file is blank/corrupt

KunLun :

I have an Spring App(running on AWS Lambda) which gets a file and uploads it on 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);

}

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.

Why is the uploaded PDF file blank?

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.

Llama :

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.

        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);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416395&siteId=1