0

I new in AWS and I have a problem with my AWS project:

I would like to create a PDF file in my Amazon S3 Bucket when I add a CSV file in the same Bucket. I am using a lambda function for this. I then use an event that calls my function when uploading a CSV file.

But I am unable to create this file.

Here is my code:

public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {
    
    private AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("us-east-1").enablePathStyleAccess().disableChunkedEncoding().build();
   
    public LambdaFunctionHandler() {}

    LambdaFunctionHandler(AmazonS3 s3) {
        this.s3Client = s3;
    }
    
    @Override
    public String handleRequest(S3Event event, Context context) throws AmazonServiceException {

        
        LambdaLogger logger = context.getLogger();      
        logger.log("Received event: " + event);           
        
        try {
            
            S3EventNotificationRecord record = event.getRecords().get(0);
            
            String bucket = record.getS3().getBucket().getName();
            String key = record.getS3().getObject().getKey();
            
            String body = (s3Client.getObjectAsString(bucket, key)).replace(';', ' ');
            System.out.println("CSV Data : " + body);
            
            String nameOfTheNewFile = "newFileObjKeyName.pdf";
           
            //PDF Create
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter.getInstance(document, new FileOutputStream("FileCreated.pdf"));
            document.open();
            document.add(new Paragraph("Data test")); // add data content to pdf
            document.close();

            PutObjectRequest request = new PutObjectRequest(bucket, nameOfTheNewFile, new File("FileCreated.pdf"));
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("plain/text");
            metadata.addUserMetadata("title", "someTitle");
            request.setMetadata(metadata);
            s3Client.putObject(request);                       
                                       
            return "ok";
        }
            
        catch (Exception  e) {
            System.err.println("Exception : " + e);
            return "err";
        }
    }
}

My lambda function is stored on another Bucket as .JAR. I have this error which is returned by my logs:

Exception : java.io.FileNotFoundException: FileCreated.pdf (Read-only file system)

I look for answers to this problem but I can not unblock myself.

Thanks all for yours helps !

5
  • Are you using PDFBox API to create the PDF? Commented Oct 22, 2021 at 13:23
  • i'm using itextPDF API. Commented Oct 22, 2021 at 13:35
  • 1
    => the FileCreated.pdf needs be in /tmp/ Commented Oct 22, 2021 at 13:40
  • Note that your Lambda function has access to at most 512MB of writable disk (in /tmp). Probably not an issue for you, but something to be aware of. Commented Oct 22, 2021 at 13:47
  • Thanks for your help, I had a hard time trying with the / tmp folder before as it works now. Commented Oct 22, 2021 at 14:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.