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 !

luk2302
  • 55,258
  • 23
  • 97
  • 137
Zekitui
  • 7
  • 1

0 Answers0