Search code examples
javaamazon-s3xlsxaspose

How to save an aspose workbook (.xlsx) to aws s3 using java?


I want to save Aspose worbook (.xlsx) to AWS S3 using java. Any help?

Providing S3 path directly to workbook.save("s3://...") will not work.

I am creating this file in AWS EMR Cluster. I can save this file in the same cluster and then move the file to S3. But I would like to know if there is any way of saving it directly to S3. I looked for answers but did not get any.


Solution

  • You can save the file in EMR Cluster and then move it to S3, then delete the file from EMR Cluster. The code snippet is given below:

    workbook.save("temp.xlsx");
    File file = new File("temp.xlsx");
    InputStream dataStream = new FileInputStream(file);
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withRegion(clientRegion)
                    .build();
    ObjectMetadata metadata = new ObjectMetadata();
    s3Client.putObject(new PutObjectRequest(bucketName, s3Key, dataStream, metadata));          
    file.delete();