Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
AWS Lambda and S3 - uploaded pdf file is blank/corrupt

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Can I say I have seen people do this allot, whereby their app, accepts a MultipartFile, and proxy upload it to S3.

Uploading to your App and then S3 honestly is the wrong approach, and has many drawbacks which negate the benefits of using S3 in the first place. Simply generate a pre-signed URL and have your user upload directly to S3. This is preferable for a few reasons but the main ones are:

  • An app which acepts MultipartFile upload can be easily DDOS'ed if not careful, this happened to our app when somebody uploaded many files at the same time whole system went down.
  • It will be MUCH slower to upload to your backend app and then upload to S3.

If you happened to be using Cognito you can also achieve this with 0 backend code using AWS Amplify. Which I highly recommend, but if not then pre-signed URL is the way to go.

over 4 years ago · Santiago Trujillo Relatório

0

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);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda