Command with file attachment

Hi,

How do you handle the situation where you have an “upload file” command requested by a client with Axon ?
I have to store document files like PDF, Images, etc. This documents are linked to a particular aggregates.

Do you attach document binary content in the command, and store the file with a particular repository in the command handlers ?
How do you construct the event (like “FileDocumentAddedToMyAggregateEvent”) ?

Thanks,

Baptiste.

Hi Baptiste,

I’ve had this requirement myself in a project. What I did there, is store the binary content in a “ContentRepository”. Based on the content you store there, the repository returns an identifier (in our case, the MD5 of the actual content stored, to prevent duplicates). In the Event, we just provided the ContentID. The meta data of the upload is stored in a query model just like you would store any other type of information. The actual file contents can be retrieved from the “ContentRepository” using the ContentID from the Event / Query Model.

Hope this helps.
Cheers,

Allard

Hi Allard and thank you for your response.
Do you call your contentRepository in the command handler (so i suppose that you transfer the binary data in the command) ?
Have you ever had memory issue using the command bus for big file ?

Baptiste.

Hi Baptiste,

usually, http file uploads are stored in temporary files. So the command would just need to have an InputStream to the contents of the file. If you need to distribute your commands, you probably want to store the content to a temporary file on the receiving end (or only do do if the file is > 1MB).
Do note that you can only read an InputStream once, so be careful with generic loggers or validators.

Cheers,

Allard

Hi,

Thanks. What i don’t understand is in case of distributed bus : How do you send the temporary file previously created from the controller to the target computer command handler, you have no information about the target because it is managed by the distributed command bus ?

Baptiste.

The fact that there is a temporary file backing an InputStream is completely transparent to the application. So in the Command’s API, you can’t see there is a file providing the actual bytes. Unfortunately, serializers don’t see that either and might try to serialize everything into a byte[]. This may become large, depending on the size of the uploaded file. So probably you want the sender to store the binary content in the repository, and include the ContentId in the command itself.

Ok, in a first time, i will left the inpustream in the command, and if there are problems with that, i will try an other solution.
Thank you.

Best regards,

Baptiste.