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-
64namespace 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
0 commit comments