Nowadays from .net developer company want they have experience in .net core + AWS .So i am thinking to share my knowledge with my friends. I will show you demo how to Configuring the AWS SDK for .NET with .NET Core.
Thank you hope it is will help you.
One of the biggest changes in .NET Core is the removal of
Configuration in .NET Core is based on key-value pairs established by configuration providers. Configuration providers read configuration data into key-value pairs from a variety of configuration sources, including command-line arguments, directory files, environment variables, and settings files.ConfigurationManager
and the standard app.config
and web.config
files that were used with .NET Framework and ASP.NET applications.
To make it easy to use the AWS SDK for .NET with .NET Core, you can use the
AWSSDK.Extensions.NETCore.Setup
NuGet package. Like many .NET Core libraries, it adds extension methods to the IConfiguration
interface to make getting the AWS configuration seamless.Using AWSSDK.Extensions.NETCore.Setup
When you create an ASP.NET Core MVC application in Visual Studio, the constructor for
Startup.cs
handles configuration by reading in various input sources from configuration providers, such as reading appsettings.json.public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
To use the
Configuration
object to get the AWS options, first add the AWSSDK.Extensions.NETCore.Setup
NuGet package. Then, add your options to the configuration file. Notice one of the files added to the ConfigurationBuilder
is called $"appsettings.{env.EnvironmentName}.json"
. If you look at the Debug tab in your project’s properties, you can see this file is set to Development. This works great for local testing because you can put your configuration in the appsettings.Development.json
file, which is read-only during local testing. When you deploy an Amazon EC2 instance that has EnvironmentName
set to Production, this file is ignored and the AWS SDK for .NET falls back to the IAM credentials and region configured for the Amazon EC2 instance.
The following configuration settings show examples of the values you can add in the
appsettings.Development.json
file in your project to supply AWS settings.{
"AWS": {
"Profile": "local-test-profile",
"Region": "us-west-2"
},
"SupportEmail": "TechSupport@example.com"
}
To access a setting in an CSHTML file, use the
Configuration
directive:@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<h1>Contact</h1>
<p>
<strong>Support:</strong> <a href='mailto:@Configuration["SupportEmail"]'>@Configuration["SupportEmail"]</a><br />
</p>
To access the AWS options set in the file from code, call the
GetAWSOptions
extension method added on IConfiguration
.
To construct a service client from these options, call
CreateServiceClient
. The following example code shows how to create an Amazon S3 service client.var options = Configuration.GetAWSOptions();
IAmazonS3 client = options.CreateServiceClient<IAmazonS3>();
You can also create multiple service clients with incompatible settings using multiple entries in the
appsettings.Development.json
file, as shown in the following examples where the configuration for service1
includes the us-west-2
Region and the configuration for service2
includes the special endpoint URL.{
"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
:var options = Configuration.GetAWSOptions("service1");
No comments:
Post a Comment