Search code examples
javaamazon-web-servicesspring-bootamazon-s3docx4j

Download a .docx file from s3, rewrite and upload to s3 again. Java 17 Springboot


I have a templates files (.docx) in a Bucket S3. These files contains text and variables that will be replaced with some content. This replacement will be do with Docx4J. Once it is replaced, I have to upload these files to S3 again.

When I get the file from s3 I use:

byte[] doc = s3Client.downloadFile(s3TemplateFolder, "example-template-en.docx", bucket);

For replace the variables in the file I use Docx4J:

  InputStream targetStream = ByteSource.wrap(doc).openStream();
  HashMap mappings = new HashMap();
  WordprocessingMLPackage wordMLPackage = Docx4J.load(targetStream);
  VariablePrepare.prepare(wordMLPackage);
  mappings.put("Download_date", "asd!");
  wordMLPackage.getMainDocumentPart().variableReplace(mappings);

The problem is when I have to upload the updated file to s3 again. S3client only support File type. I don't know how should I do for convert WordprocessingMLPackage to a File again.

If you can think of another way to do this, you are welcome :)

Thank you so much.


Solution

  • Not an expert on docx4j but you do have the save method on the WordprocessingMLPackage class that allows you to save to a file.

    https://javadoc.io/doc/org.docx4j/docx4j/latest/org/docx4j/openpackaging/packages/OpcPackage.html

    The OpcPackage is a parent class of WordprocessingMLPackage so you should be able to call the save method that saves to a file.

    public void save(File file) throws Docx4JException

    Convenience method to save a WordprocessingMLPackage or PresentationMLPackage to a File. If the file ends with .xml, use Flat OPC XML format; otherwise zip it up.

    Throws: Docx4JException

    You could use this to save the file and then upload it to S3.

    You could also save the file to a byteArray using this method.

    public void save(OutputStream outStream) throws Docx4JException

    Save this pkg to an OutputStream in the usual zipped up format (Docx4J.FLAG_SAVE_ZIP_FILE)

    Throws: Docx4JException Since: 3.1.0

    And then use the contents of the OutputStream to save to S3.