Skip to content

Commit 470c90a

Browse files
committed
add httpfactory
1 parent f9e0073 commit 470c90a

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/Basics.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="4.0.2" />
1313
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
1516
</ItemGroup>
1617

1718
</Project>

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/CreatePresignedPostBasics.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using Microsoft.Extensions.Hosting;
5-
64
namespace S3Scenarios;
75

86
// snippet-start:[S3.dotnetv4.CreatePresignedPostBasics]
@@ -18,6 +16,7 @@ public class CreatePresignedPostBasics
1816
public static ILogger<CreatePresignedPostBasics> _logger = null!;
1917
public static S3Wrapper _s3Wrapper = null!;
2018
public static UiMethods _uiMethods = null!;
19+
public static IHttpClientFactory _httpClientFactory = null!;
2120
public static bool _isInteractive = true;
2221
public static string? _bucketName;
2322
public static string? _objectKey;
@@ -34,8 +33,8 @@ public static void SetUpServices(IHost host)
3433
});
3534
_logger = new Logger<CreatePresignedPostBasics>(loggerFactory);
3635

37-
// Now the client is available for injection.
3836
_s3Wrapper = host.Services.GetRequiredService<S3Wrapper>();
37+
_httpClientFactory = host.Services.GetRequiredService<IHttpClientFactory>();
3938
_uiMethods = new UiMethods();
4039
}
4140

@@ -46,14 +45,14 @@ public static void SetUpServices(IHost host)
4645
/// <returns>A Task object.</returns>
4746
public static async Task Main(string[] args)
4847
{
49-
// Check for non-interactive mode
5048
_isInteractive = !args.Contains("--non-interactive");
5149

5250
// Set up dependency injection for Amazon S3
5351
using var host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
5452
.ConfigureServices((_, services) =>
5553
services.AddAWSService<IAmazonS3>()
5654
.AddTransient<S3Wrapper>()
55+
.AddHttpClient()
5756
)
5857
.Build();
5958

@@ -151,7 +150,6 @@ private static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync(
151150
Console.WriteLine($"Creating presigned POST URL for {_bucketName}/{_objectKey}");
152151
Console.WriteLine($"Expiration: {expiration} UTC");
153152

154-
// Get S3 client from S3Wrapper
155153
var s3Client = _s3Wrapper.GetS3Client();
156154

157155
var response = await _s3Wrapper.CreatePresignedPostAsync(
@@ -207,8 +205,7 @@ private static async Task UploadFileAsync(CreatePresignedPostResponse response)
207205
{
208206
_logger.LogInformation("Uploading file {filePath} using presigned POST URL", filePath);
209207

210-
// Create HttpClient to send the request
211-
using var httpClient = new HttpClient();
208+
using var httpClient = _httpClientFactory.CreateClient();
212209
using var formContent = new MultipartFormDataContent();
213210

214211
// Add all the fields from the presigned POST response

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/Usings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
global using Microsoft.Extensions.Logging;
99
global using Microsoft.Extensions.DependencyInjection;
1010
global using Amazon.S3.Util;
11+
global using Microsoft.Extensions.Hosting;
12+
1113

0 commit comments

Comments
 (0)