BLOB 存储: 创建自定义提供程序

本文档通过一个示例说明如何为BLOB存储系统创建新的存储提供程序.

阅读BLOB存储文档了解如何使用BLOB存储系统. 本文档仅介绍如何创建新存储提供程序.

示例实现

第一步是创建一个实现 IBlobProvider 接口或 BlobProviderBase 抽象类继承的类.

  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 继承 BlobProviderBase 并覆盖 abstract 方法. 实际的实现取决于你.
  • 实现 ITransientDependency 接口将这个类注做为瞬态服务注册到依赖注入系统.

注意: 命名约定很重要. 如果类名没有以 BlobProvider 结尾,则必须手动注册/公开你的服务为 IBlobProvider.

这是所有. 现在你可以配置容器(在模块ConfigureServices 方法中)使用 MyCustomBlobProvider 类:

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

如果你想配置特定的容器,请参阅BLOB存储文档.

BlobContainerConfiguration 扩展方法

如果你想提供一个更简单的配置方式,可以为 BlobContainerConfiguration 类创建一个扩展方法:

  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. }

然后你可以使用扩展方法更容易地配置容器:

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

额外的配置选项

BlobContainerConfiguration 允许添加/删除提供程序特定的配置对象. 如果你的提供者需要额外的配置,你可以为 BlobContainerConfiguration 创建一个包装类提供的类型安全配置选项:

  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. }

然后你可以这样更改 MyBlobContainerConfigurationExtensions 类:

  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. }
  • UseMyCustomBlobProvider 方法添加了一个参数,允许开发人员设置其他选项.
  • 添加了一个新的 GetMyCustomBlobProviderConfiguration 方法,该方法将在 MyCustomBlobProvider 类内使用获取配置的值.

然后任何人都可以如下设置 MyOption1:

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

最后你可以使用 GetMyCustomBlobProviderConfiguration 方法访问额外的选项:

  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. }

贡献?

如果你创建了一个新的提供程序,并且认为它对其他开发者有用,请考虑为GitHub上的ABP框架做出贡献.