BLOB Storing: Creating a Custom Provider

This document explains how you can create a new storage provider for the BLOB storing system with an example.

Read the BLOB Storing document to understand how to use the BLOB storing system. This document only covers how to create a new storage provider.

Example Implementation

The first step is to create a class implements the IBlobProvider interface or inherit from the BlobProviderBase abstract class.

  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using Volo.Abp.BlobStoring;
  4. using Volo.Abp.DependencyInjection;
  5. namespace AbpDemo
  6. {
  7. public class MyCustomBlobProvider : BlobProviderBase, ITransientDependency
  8. {
  9. public override Task SaveAsync(BlobProviderSaveArgs args)
  10. {
  11. //TODO...
  12. }
  13. public override Task<bool> DeleteAsync(BlobProviderDeleteArgs args)
  14. {
  15. //TODO...
  16. }
  17. public override Task<bool> ExistsAsync(BlobProviderExistsArgs args)
  18. {
  19. //TODO...
  20. }
  21. public override Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
  22. {
  23. //TODO...
  24. }
  25. }
  26. }
  • MyCustomBlobProvider inherits from the BlobProviderBase and overrides the abstract methods. The actual implementation is up to you.
  • Implementing ITransientDependency registers this class to the Dependency Injection system as a transient service.

Notice: Naming conventions are important. If your class name doesn’t end with BlobProvider, you must manually register/expose your service for the IBlobProvider.

That’s all. Now, you can configure containers (inside the ConfigureServices method of your module) to use the MyCustomBlobProvider class:

  1. Configure<AbpBlobStoringOptions>(options =>
  2. {
  3. options.Containers.ConfigureDefault(container =>
  4. {
  5. container.ProviderType = typeof(MyCustomBlobProvider);
  6. });
  7. });

See the BLOB Storing document if you want to configure a specific container.

BlobContainerConfiguration Extension Method

If you want to provide a simpler configuration, create an extension method for the BlobContainerConfiguration class:

  1. public static class MyBlobContainerConfigurationExtensions
  2. {
  3. public static BlobContainerConfiguration UseMyCustomBlobProvider(
  4. this BlobContainerConfiguration containerConfiguration)
  5. {
  6. containerConfiguration.ProviderType = typeof(MyCustomBlobProvider);
  7. return containerConfiguration;
  8. }
  9. }

Then you can configure containers easier using the extension method:

  1. Configure<AbpBlobStoringOptions>(options =>
  2. {
  3. options.Containers.ConfigureDefault(container =>
  4. {
  5. container.UseMyCustomBlobProvider();
  6. });
  7. });

Extra Configuration Options

BlobContainerConfiguration allows to add/remove provider specific configuration objects. If your provider needs to additional configuration, you can create a wrapper class to the BlobContainerConfiguration for a type-safe configuration option:

  1. public class MyCustomBlobProviderConfiguration
  2. {
  3. public string MyOption1
  4. {
  5. get => _containerConfiguration
  6. .GetConfiguration<string>("MyCustomBlobProvider.MyOption1");
  7. set => _containerConfiguration
  8. .SetConfiguration("MyCustomBlobProvider.MyOption1", value);
  9. }
  10. private readonly BlobContainerConfiguration _containerConfiguration;
  11. public MyCustomBlobProviderConfiguration(
  12. BlobContainerConfiguration containerConfiguration)
  13. {
  14. _containerConfiguration = containerConfiguration;
  15. }
  16. }

Then you can change the MyBlobContainerConfigurationExtensions class like that:

  1. public static class MyBlobContainerConfigurationExtensions
  2. {
  3. public static BlobContainerConfiguration UseMyCustomBlobProvider(
  4. this BlobContainerConfiguration containerConfiguration,
  5. Action<MyCustomBlobProviderConfiguration> configureAction)
  6. {
  7. containerConfiguration.ProviderType = typeof(MyCustomBlobProvider);
  8. configureAction.Invoke(
  9. new MyCustomBlobProviderConfiguration(containerConfiguration)
  10. );
  11. return containerConfiguration;
  12. }
  13. public static MyCustomBlobProviderConfiguration GetMyCustomBlobProviderConfiguration(
  14. this BlobContainerConfiguration containerConfiguration)
  15. {
  16. return new MyCustomBlobProviderConfiguration(containerConfiguration);
  17. }
  18. }
  • Added an action parameter to the UseMyCustomBlobProvider method to allow developers to set the additional options.
  • Added a new GetMyCustomBlobProviderConfiguration method to be used inside MyCustomBlobProvider class to obtain the configured values.

Then anyone can set the MyOption1 as shown below:

  1. Configure<AbpBlobStoringOptions>(options =>
  2. {
  3. options.Containers.ConfigureDefault(container =>
  4. {
  5. container.UseMyCustomBlobProvider(provider =>
  6. {
  7. provider.MyOption1 = "my value";
  8. });
  9. });
  10. });

Finally, you can access to the extra options using the GetMyCustomBlobProviderConfiguration method:

  1. public class MyCustomBlobProvider : BlobProviderBase, ITransientDependency
  2. {
  3. public override Task SaveAsync(BlobProviderSaveArgs args)
  4. {
  5. var config = args.Configuration.GetMyCustomBlobProviderConfiguration();
  6. var value = config.MyOption1;
  7. //...
  8. }
  9. }

Contribute?

If you create a new provider and you think it can be useful for other developers, please consider to contribute to the ABP Framework on GitHub.