Home » Java » PDF is blank upon upload to AWS S3 via AWS Lambda Function using Java/Spring – Stack Overflow

PDF is blank upon upload to AWS S3 via AWS Lambda Function using Java/Spring – Stack Overflow

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a pretty simple function that uploads a file to an aws s3 bucket (https://codedestine.com/aws-s3-putobject-java/) via api gateway/lambda but the file shows up as blank within the aws s3 bucket. It is indeed getting uploaded but there is no content on the blank pdf pages.

public static void uploadFile2(MultipartFile file,  String fileName) throws IOException {
        String path2 = System.getProperty("java.io.tmpdir", "/tmp");
        File file2 = new File(path2 + "/" + fileName);
        OutputStream ops = new FileOutputStream(file2);
        ops.write(file.getBytes());
        s3client.putObject("fakebucketname",fileName, file2);
    }

Please let me know if there are any solutions to this issue, any insight/comments are appreciated. Best

Answers:

You’re using an output stream as input to the upload request. Just use File, and include content type, for example:

File file = new File(fileName);
PutObjectRequest request = new PutObjectRequest("bucketname", "keyname", file);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/pdf");
request.setMetadata(metadata);
s3Client.putObject(request);