File Storage Type Based on App Property

Hi. It’s great that Cuba supports a standard file storage and Amazon S3. I was able to set up S3 storage but I would like to make the selection of file storage type based on an app property. So by leveraging the contexts, I can use standard file storage when developing/deploying locally and S3 storage when deploying to Heroku. I imagine other people would like to do this too, right?

My idea was to create my own class that implements FileStorageAPI and routes the method calls to the appropriate file storage based on the app property. But I’m having trouble. Calling the constructors for FileStorage or AmazonS3FileStorage doesn’t seem to work. I am not very familiar with Spring. I tried loading both beans but the system didn’t like that either.

Any help is appreciated. Thanks.


I think you can use Spring feature - org.springframework.beans.factory.config.BeanFactoryPostProcessor

It runs during Spring context initialization. It can use AppContext.getProperty() properties.
It can replace definition of cuba_FileStorage bean based on condition.

Like this:

public class MyReplacer implements BeanFactoryPostProcessor {
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
       BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
  if (...) {
      BeanDefinition definition = new RootBeanDefinition(MyOwnFileStorageImplementation.class);
      // set properties, constructor arguments
      registry.registerBeanDefinition(FileStorageAPI.NAME, definition);

This worked great. Thanks!!