How to get a server-side file into CUBA's filestorage?

I’m close to working out how to do it but just in case someone else has already done it and wants to share the solution - how would one go about getting a file that exists on the server’s local filesystem into CUBA’s filestorage?

We’re building data loaders/converters from our old system to the new CUBA system and need this ability to convert in documents (files) that are attached to notes, implemented via CUBA’s filestorage in the new system.

Hi Jon,

There is an option to upload files using CUBA standard screen “Administration → External Files”.

If you want to copy a lot of files, you can create s service that will copy them to CUBA file storage. Those files must be placed to the server where CUBA core application is running. After that, you will be able to copy files from the file system. The code in the service might look like this. You can obviously modify it and specify a directory where all documents are stored, attach file descriptor entity to your CUBA entities, etc.

    protected DataManager dataManager;
    private FileStorageAPI fileStorageAPI;
    private Logger log;
    private TimeSource timeSource;

    public FileDescriptor copyFileUsingStream(String source) throws FileStorageException
        File file = new File(source);
        try (InputStream inputStream = new FileInputStream(file);) {
            FileDescriptor fd = dataManager.create(FileDescriptor.class);

            fileStorageAPI.saveStream(fd, inputStream);
            return dataManager.commit(fd);

        } catch (IOException e) {
            log.error("Error", e);
            throw new RuntimeException("Cannot copy file to the file storage", e);

You can find more info in the documentation: File Storage - CUBA Platform. Developer’s Manual

1 Like

Thank you @belyaev - I basically worked it out from the documentation and arrived at a similar solution.