Open Closed

Access ISettingsManager from Module ConfigureServices #2025


User avatar
0
riley.trevillion.spatialhub4d created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v4.3.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no

Hello I'm trying to configure the AWS Blob Storage Provider within my application module with values that come from the ABP Settings Service.

The documentation for the AWS storage provider says that I can only configure these options within a modules ConfigureServices method. https://docs.abp.io/en/abp/latest/Blob-Storing-Aws My understanding is that the ConfigureServices method is where dependency injection is being set up and I won't be able to access the ABP Settings manager service to look up the values I need from this location. Is this true, or is there a way to do this?

To illustrate what I am after, see the picture below. I know this will not work, but it's in the realm of what I am trying to achieve.

Is there any way I can get values specifically from the ABP Settings Service (i.e from the database at runtime) and use those to configure the AWS Blob Storage provider? I would like to avoid accessing these values from appsettings.json via IConfiguration if possible as there is a requirement for these options to be changeable at runtime from the UI

Any assistance appreciated, thankyou


4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can try to override the IBlobContainerConfigurationProvider service and change some configuration in the Get method.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/DefaultBlobContainerConfigurationProvider.cs#L17

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainerFactory.cs#L42

  • User Avatar
    0
    riley.trevillion.spatialhub4d created

    Hi @maliming

    Thank you for the reply

    I've taken a look at how to implement your suggestion but I'm having a bit of trouble understanding how to override the files and class members you've provided. I'm not sure how the configuration overrides fit in between the various blob storage and AWS provider files and how they all wire up.

    Would you be able to provide a little more information on this? Even a little bit of example code to point me in the right direction would be very helpful and appreciated.

    Thank you again

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you try this?

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IBlobContainerConfigurationProvider))]
    public class MyBlobContainerConfigurationProvider : DefaultBlobContainerConfigurationProvider
    {
        public MyBlobContainerConfigurationProvider(IOptions<AbpBlobStoringOptions> options)
            : base(options)
        {
        }
    
        public override BlobContainerConfiguration Get(string name)
        {
            if (name == "MyBlobContainer")
            {
                var configure = base.Get(name);
                var aws = configure.GetAwsConfiguration();
                aws.AccessKeyId = "";
            }
    
            return base.Get(name);
        }
    }
    
  • User Avatar
    0
    riley.trevillion.spatialhub4d created

    Hi @maliming

    Thank you for that code example. I was able to achieve what I needed with what you provided. I'll put the completed pieces here for anyone else who may need to do the same thing.

    <br> MyAwsBlobContainer.cs

    [BlobContainerName("my-aws-blob-container")]
    public class MyAwsBlobContainer
    {
    }
    

    <br> <br> CustomAwsBlobConfigurationProvider.cs

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IBlobContainerConfigurationProvider))]
    public class CustomAwsBlobConfigurationProvider : DefaultBlobContainerConfigurationProvider
    {
        private readonly ISettingManager _settingManager;
    
        public CustomAwsBlobConfigurationProvider(
            IOptions<AbpBlobStoringOptions> options,
            ISettingManager settingManager
        ) : base(options)
        {
            _settingManager = settingManager;
        }
    
        public override BlobContainerConfiguration Get(string name)
        {
            if (name == "my-aws-blob-container")
            {
                var configure = base.Get(name);
                var aws = configure.GetAwsConfiguration();
    
                aws.AccessKeyId = _settingManager.GetOrNullGlobalAsync(SharedSettings.PropertyReportStorage.AwsAccessKeyId).GetAwaiter().GetResult();
                aws.SecretAccessKey = _settingManager.GetOrNullGlobalAsync(SharedSettings.PropertyReportStorage.AwsSecretAccessKey).GetAwaiter().GetResult();
                aws.Region = _settingManager.GetOrNullGlobalAsync(SharedSettings.PropertyReportStorage.AwsRegion).GetAwaiter().GetResult();
                aws.CreateContainerIfNotExists = true;
            }
    
            return base.Get(name);
        }
    }
    

    <br> <br> SharedApplicationModule.cs

    [DependsOn(
        typeof(SharedDomainModule),
        typeof(SharedApplicationContractsModule),
        typeof(AbpDddApplicationModule),
        typeof(AbpAutoMapperModule),
        typeof(AbpBlobStoringModule),
        typeof(AbpBlobStoringAwsModule)
    )]
    public class SharedApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBlobStoringOptions>(options =>
            {
                options.Containers.Configure<MyAwsBlobContainer>(container =>
                {
                    container.UseAws(aws => { });
                });
            });
        }
    }
    

    <br> Thanks again

Made with ❤️ on ABP v9.1.0-preview. Updated on December 10, 2024, 06:38