这是我为多个 S3 客户端制作的两个接口:
public interface IAmazonS3First : IAmazonS3
{
}
public interface IAmazonS3Second : IAmazonS3
{
}
在 Program.cs 中,我添加了以下 sdetup 来设置依赖注入:
var awsOption = configuration.GetAWSOptions();
awsOption.Credentials = new BasicAWSCredentials(configuration["FirstAccessKey"], configuration["FirstSecretKey"]);
services.AddAWSService<IAmazonS3First>(awsOption);
awsOption = configuration.GetAWSOptions();
awsOption.Credentials = new BasicAWSCredentials(configuration["SecondAccessKey"], configuration["SecondSecretKey"]);
services.AddAWSService<IAmazonS3Second>(awsOption);
在目标类中,我想利用这两个客户端并设置了以下代码:
private readonly IAmazonS3First _s3FirstClient;
private readonly IAmazonS3Second _s3SecondClient;
public MyService(IAmazonS3First s3FirstClient, IAmazonS3Second s3SecondClient)
{
_s3FirstClient = s3FirstClient;
_s3SecondClient = s3SecondClient;
问题:当我调用该服务时,出现以下错误:
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateConfig(Type serviceInterfaceType, AWSOptions options)
at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateServiceClient(ILogger logger, Type serviceInterfaceType, AWSOptions options)
at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateServiceClient(IServiceProvider provider)
你能分享一下我在添加注射时做错了什么吗?
我的目标是使用两个不同的 Amazon S3 客户端(两组不同的 AWS 访问密钥/秘密密钥)
GetAWSOptions() 方法用于设置包含一些选项的部分(来自您的配置)。
https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html
{
"service1": {
"Profile": "default",
"Region": "us-west-2"
},
"service2": {
"Profile": "default",
"ServiceURL": "URL"
}
}
You can then get the options for a specific service by using the entry in the JSON file. For example, to get the settings for service1 use the following.
var options = Configuration.GetAWSOptions("service1");
不带参数使用时,默认为 DEFAULT_CONFIG_SECTION = "AWS"。 您的配置中有“AWS”部分吗?
喜欢
{
"AWS": {
"Profile": "local-test-profile",
"Region": "us-west-2"
},
"SupportEmail": "[email protected]"
}