Skip to content

Commit a37a081

Browse files
committed
dotnet format
1 parent 6e21ff3 commit a37a081

File tree

6 files changed

+49
-51
lines changed

6 files changed

+49
-51
lines changed

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/CreatePresignedPostBasics.cs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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("\nUploading 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]

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/S3Wrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public IAmazonS3 GetS3Client()
3131
{
3232
return _s3Client;
3333
}
34-
34+
3535
// snippet-start:[S3.dotnetv4.Scenario_CreatePresignedPostAsync]
3636
/// <summary>
3737
/// Create a presigned POST URL with conditions.
@@ -171,4 +171,4 @@ public async Task<GetObjectMetadataResponse> GetObjectMetadataAsync(
171171

172172
return await _s3Client.GetObjectMetadataAsync(request);
173173
}
174-
}
174+
}

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/UiMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ public string GetUserInput(string prompt, string? defaultValue = null, bool isIn
6969
return defaultValue ?? "";
7070
}
7171
}
72-
}
72+
}

dotnetv4/S3/Scenarios/S3_CreatePresignedPost/Usings.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
global using System.Net.Http.Headers;
66
global using Amazon.S3;
77
global using Amazon.S3.Model;
8-
global using Microsoft.Extensions.Logging;
9-
global using Microsoft.Extensions.DependencyInjection;
108
global using Amazon.S3.Util;
9+
global using Microsoft.Extensions.DependencyInjection;
1110
global using Microsoft.Extensions.Hosting;
12-
13-
11+
global using Microsoft.Extensions.Logging;

dotnetv4/S3/Tests/CreatePresignedPostBasicsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ public async Task TestScenario()
2424
var loggerScenarioMock = new Mock<ILogger<S3Scenarios.CreatePresignedPostBasics>>();
2525
var loggerWrapperMock = new Mock<ILogger<S3Wrapper>>();
2626
var uiMethods = new S3Scenarios.UiMethods();
27-
27+
2828
_client = new AmazonS3Client();
2929
_s3Wrapper = new S3Wrapper(_client, loggerWrapperMock.Object);
30-
30+
3131
// Set up the static fields directly
3232
S3Scenarios.CreatePresignedPostBasics._logger = loggerScenarioMock.Object;
3333
S3Scenarios.CreatePresignedPostBasics._s3Wrapper = _s3Wrapper;
3434
S3Scenarios.CreatePresignedPostBasics._uiMethods = uiMethods;
3535
S3Scenarios.CreatePresignedPostBasics._isInteractive = false;
36-
36+
3737
// Set up verification for error logging.
3838
loggerScenarioMock.Setup(logger => logger.Log(
3939
It.Is<LogLevel>(logLevel => logLevel == LogLevel.Error),
@@ -74,4 +74,4 @@ public async Task TestScenario()
7474
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
7575
Times.Never);
7676
}
77-
}
77+
}

dotnetv4/S3/Tests/Usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
global using Amazon.S3;
55
global using Microsoft.Extensions.Logging;
66
global using Moq;
7-
global using Xunit;
87
global using S3Scenarios;
8+
global using Xunit;

0 commit comments

Comments
 (0)