@@ -46,7 +46,7 @@ public static void SetUpServices(IHost host)
4646 public static async Task Main ( string [ ] args )
4747 {
4848 _isInteractive = ! args . Contains ( "--non-interactive" ) ;
49-
49+
5050 // Set up dependency injection for Amazon S3
5151 using var host = Microsoft . Extensions . Hosting . Host . CreateDefaultBuilder ( args )
5252 . ConfigureServices ( ( _ , services ) =>
@@ -57,48 +57,48 @@ public static async Task Main(string[] args)
5757 . Build ( ) ;
5858
5959 SetUpServices ( host ) ;
60-
60+
6161 try
6262 {
6363 // Display overview
6464 _uiMethods . DisplayOverview ( ) ;
6565 _uiMethods . PressEnter ( _isInteractive ) ;
66-
66+
6767 // Step 1: Create bucket
6868 await CreateBucketAsync ( ) ;
6969 _uiMethods . PressEnter ( _isInteractive ) ;
70-
70+
7171 // Step 2: Create presigned URL
7272 _uiMethods . DisplayTitle ( "Step 2: Create presigned POST URL" ) ;
7373 var response = await CreatePresignedPostAsync ( ) ;
7474 _uiMethods . PressEnter ( _isInteractive ) ;
75-
75+
7676 // Step 3: Display URL and fields
7777 _uiMethods . DisplayTitle ( "Step 3: Presigned POST URL details" ) ;
7878 DisplayPresignedPostFields ( response ) ;
7979 _uiMethods . PressEnter ( _isInteractive ) ;
80-
80+
8181 // Step 4: Upload file
8282 _uiMethods . DisplayTitle ( "Step 4: Upload test file using presigned POST URL" ) ;
8383 await UploadFileAsync ( response ) ;
8484 _uiMethods . PressEnter ( _isInteractive ) ;
85-
85+
8686 // Step 5: Verify file exists
8787 await VerifyFileExistsAsync ( ) ;
8888 _uiMethods . PressEnter ( _isInteractive ) ;
89-
89+
9090 // Step 6: Cleanup
9191 _uiMethods . DisplayTitle ( "Step 6: Clean up resources" ) ;
9292 await CleanupAsync ( ) ;
93-
93+
9494 _uiMethods . DisplayTitle ( "S3 Presigned POST Scenario completed successfully!" ) ;
9595 _uiMethods . PressEnter ( _isInteractive ) ;
9696 }
9797 catch ( Exception ex )
9898 {
9999 _logger . LogError ( ex , "Error in scenario" ) ;
100100 Console . WriteLine ( $ "Error: { ex . Message } ") ;
101-
101+
102102 // Attempt cleanup if there was an error
103103 if ( ! string . IsNullOrEmpty ( _bucketName ) )
104104 {
@@ -115,26 +115,26 @@ public static async Task Main(string[] args)
115115 private static async Task CreateBucketAsync ( )
116116 {
117117 _uiMethods . DisplayTitle ( "Step 1: Create an S3 bucket" ) ;
118-
118+
119119 // Generate a default bucket name for the scenario
120120 var defaultBucketName = $ "presigned-post-demo-{ DateTime . Now : yyyyMMddHHmmss} ". ToLower ( ) ;
121-
121+
122122 // Prompt user for bucket name or use default in non-interactive mode
123123 _bucketName = _uiMethods . GetUserInput (
124- $ "Enter S3 bucket name (or press Enter for '{ defaultBucketName } '): ",
125- defaultBucketName ,
124+ $ "Enter S3 bucket name (or press Enter for '{ defaultBucketName } '): ",
125+ defaultBucketName ,
126126 _isInteractive ) ;
127-
127+
128128 // Basic validation to ensure bucket name is not empty
129129 if ( string . IsNullOrWhiteSpace ( _bucketName ) )
130130 {
131131 _bucketName = defaultBucketName ;
132132 }
133-
133+
134134 Console . WriteLine ( $ "Creating bucket: { _bucketName } ") ;
135-
135+
136136 await _s3Wrapper . CreateBucketAsync ( _bucketName ) ;
137-
137+
138138 Console . WriteLine ( $ "Successfully created bucket: { _bucketName } ") ;
139139 }
140140
@@ -146,15 +146,15 @@ private static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync(
146146 {
147147 _objectKey = "example-upload.txt" ;
148148 var expiration = DateTime . UtcNow . AddMinutes ( 10 ) ; // Short expiration for the demo
149-
149+
150150 Console . WriteLine ( $ "Creating presigned POST URL for { _bucketName } /{ _objectKey } ") ;
151151 Console . WriteLine ( $ "Expiration: { expiration } UTC") ;
152-
152+
153153 var s3Client = _s3Wrapper . GetS3Client ( ) ;
154-
154+
155155 var response = await _s3Wrapper . CreatePresignedPostAsync (
156156 s3Client , _bucketName ! , _objectKey , expiration ) ;
157-
157+
158158 Console . WriteLine ( "Successfully created presigned POST URL" ) ;
159159 return response ;
160160 }
@@ -164,18 +164,18 @@ private static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync(
164164 /// </summary>
165165 private static async Task UploadFileAsync ( CreatePresignedPostResponse response )
166166 {
167-
167+
168168 // Create a temporary test file to upload
169169 string testFilePath = Path . GetTempFileName ( ) ;
170170 string testContent = "This is a test file for the S3 presigned POST scenario." ;
171-
171+
172172 await File . WriteAllTextAsync ( testFilePath , testContent ) ;
173173 Console . WriteLine ( $ "Created test file at: { testFilePath } ") ;
174-
174+
175175 // Upload the file using the presigned POST URL
176176 Console . WriteLine ( "\n Uploading file using the presigned POST URL..." ) ;
177177 var uploadResult = await UploadFileWithPresignedPostAsync ( response , testFilePath ) ;
178-
178+
179179 // Display the upload result
180180 if ( uploadResult . Success )
181181 {
@@ -188,7 +188,7 @@ private static async Task UploadFileAsync(CreatePresignedPostResponse response)
188188 Console . WriteLine ( $ "Error: { uploadResult . Response } ") ;
189189 throw new Exception ( "File upload failed" ) ;
190190 }
191-
191+
192192 // Clean up the temporary file
193193 File . Delete ( testFilePath ) ;
194194 Console . WriteLine ( "Temporary file deleted" ) ;
@@ -198,36 +198,36 @@ private static async Task UploadFileAsync(CreatePresignedPostResponse response)
198198 /// Helper method to upload a file using a presigned POST URL.
199199 /// </summary>
200200 private static async Task < ( bool Success , HttpStatusCode StatusCode , string Response ) > UploadFileWithPresignedPostAsync (
201- CreatePresignedPostResponse response ,
201+ CreatePresignedPostResponse response ,
202202 string filePath )
203203 {
204204 try
205205 {
206206 _logger . LogInformation ( "Uploading file {filePath} using presigned POST URL" , filePath ) ;
207-
207+
208208 using var httpClient = _httpClientFactory . CreateClient ( ) ;
209209 using var formContent = new MultipartFormDataContent ( ) ;
210-
210+
211211 // Add all the fields from the presigned POST response
212212 foreach ( var field in response . Fields )
213213 {
214214 formContent . Add ( new StringContent ( field . Value ) , field . Key ) ;
215215 }
216-
216+
217217 // Add the file content
218218 var fileStream = File . OpenRead ( filePath ) ;
219219 var fileName = Path . GetFileName ( filePath ) ;
220220 var fileContent = new StreamContent ( fileStream ) ;
221221 fileContent . Headers . ContentType = new MediaTypeHeaderValue ( "text/plain" ) ;
222222 formContent . Add ( fileContent , "file" , fileName ) ;
223-
223+
224224 // Send the POST request
225225 var httpResponse = await httpClient . PostAsync ( response . Url , formContent ) ;
226226 var responseContent = await httpResponse . Content . ReadAsStringAsync ( ) ;
227-
227+
228228 // Log and return the result
229229 _logger . LogInformation ( "Upload completed with status code {statusCode}" , httpResponse . StatusCode ) ;
230-
230+
231231 return ( httpResponse . IsSuccessStatusCode , httpResponse . StatusCode , responseContent ) ;
232232 }
233233 catch ( Exception ex )
@@ -243,13 +243,13 @@ private static async Task UploadFileAsync(CreatePresignedPostResponse response)
243243 private static async Task VerifyFileExistsAsync ( )
244244 {
245245 _uiMethods . DisplayTitle ( "Step 5: Verify uploaded file exists" ) ;
246-
246+
247247 Console . WriteLine ( $ "Checking if file exists at { _bucketName } /{ _objectKey } ...") ;
248-
248+
249249 try
250250 {
251251 var metadata = await _s3Wrapper . GetObjectMetadataAsync ( _bucketName ! , _objectKey ! ) ;
252-
252+
253253 Console . WriteLine ( $ "File verification successful! File exists in the bucket.") ;
254254 Console . WriteLine ( $ "File size: { metadata . ContentLength } bytes") ;
255255 Console . WriteLine ( $ "File type: { metadata . Headers . ContentType } ") ;
@@ -282,7 +282,7 @@ private static async Task CleanupAsync()
282282 {
283283 Console . WriteLine ( $ "Deleting bucket { _bucketName } and its contents...") ;
284284 bool result = await _s3Wrapper . DeleteBucketAsync ( _bucketName ) ;
285-
285+
286286 if ( result )
287287 {
288288 Console . WriteLine ( "Bucket deleted successfully" ) ;
@@ -294,4 +294,4 @@ private static async Task CleanupAsync()
294294 }
295295 }
296296}
297- // snippet-end:[S3.dotnetv4.CreatePresignedPostBasics]
297+ // snippet-end:[S3.dotnetv4.CreatePresignedPostBasics]
0 commit comments