SDK for .NET (v4)를 사용한 Amazon S3 예제 - AWS SDK for .NET (V4)

버전 4(V4) AWS SDK for .NET 가 릴리스되었습니다.

변경 사항 해제 및 애플리케이션 마이그레이션에 대한 자세한 내용은 마이그레이션 주제를 참조하세요.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

SDK for .NET (v4)를 사용한 Amazon S3 예제

다음 코드 예제에서는 Amazon S3에서 AWS SDK for .NET (v4)를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 관련 시나리오의 컨텍스트에 따라 표시되며, 개별 서비스 함수를 직접적으로 호출하는 방법을 보여줍니다.

시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.

각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.

작업

다음 코드 예시는 CreatePresignedPost의 사용 방법을 보여 줍니다.

SDK for .NET (v4)
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

미리 서명된 POST URL을 생성합니다.

/// <summary> /// Create a presigned POST URL with conditions. /// </summary> /// <param name="s3Client">The Amazon S3 client.</param> /// <param name="bucketName">The name of the bucket.</param> /// <param name="objectKey">The object key (path) where the uploaded file will be stored.</param> /// <param name="expires">When the presigned URL expires.</param> /// <param name="fields">Dictionary of fields to add to the form.</param> /// <param name="conditions">List of conditions to apply.</param> /// <returns>A CreatePresignedPostResponse object with URL and form fields.</returns> public async Task<CreatePresignedPostResponse> CreatePresignedPostAsync( IAmazonS3 s3Client, string bucketName, string objectKey, DateTime expires, Dictionary<string, string>? fields = null, List<S3PostCondition>? conditions = null) { var request = new CreatePresignedPostRequest { BucketName = bucketName, Key = objectKey, Expires = expires }; // Add custom fields if provided if (fields != null) { foreach (var field in fields) { request.Fields.Add(field.Key, field.Value); } } // Add conditions if provided if (conditions != null) { foreach (var condition in conditions) { request.Conditions.Add(condition); } } return await s3Client.CreatePresignedPostAsync(request); }

시나리오

다음 코드 예제는 Amazon S3에 대해 미리 서명된 URL을 생성하고 객체를 업로드하는 방법을 보여줍니다.

SDK for .NET (v4)
참고

GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요.

직접 브라우저 업로드에 미리 서명된 POST URLs을 생성하고 사용합니다.

/// <summary> /// Scenario demonstrating the complete workflow for presigned POST URLs: /// 1. Create an S3 bucket /// 2. Create a presigned POST URL /// 3. Upload a file using the presigned POST URL /// 4. Clean up resources /// </summary> public class CreatePresignedPostBasics { public static ILogger<CreatePresignedPostBasics> _logger = null!; public static S3Wrapper _s3Wrapper = null!; public static UiMethods _uiMethods = null!; public static IHttpClientFactory _httpClientFactory = null!; public static bool _isInteractive = true; public static string? _bucketName; public static string? _objectKey; /// <summary> /// Set up the services and logging. /// </summary> /// <param name="host">The IHost instance.</param> public static void SetUpServices(IHost host) { var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); _logger = new Logger<CreatePresignedPostBasics>(loggerFactory); _s3Wrapper = host.Services.GetRequiredService<S3Wrapper>(); _httpClientFactory = host.Services.GetRequiredService<IHttpClientFactory>(); _uiMethods = new UiMethods(); } /// <summary> /// Perform the actions defined for the Amazon S3 Presigned POST scenario. /// </summary> /// <param name="args">Command line arguments.</param> /// <returns>A Task object.</returns> public static async Task Main(string[] args) { _isInteractive = !args.Contains("--non-interactive"); // Set up dependency injection for Amazon S3 using var host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonS3>() .AddTransient<S3Wrapper>() .AddHttpClient() ) .Build(); SetUpServices(host); try { // Display overview _uiMethods.DisplayOverview(); _uiMethods.PressEnter(_isInteractive); // Step 1: Create bucket await CreateBucketAsync(); _uiMethods.PressEnter(_isInteractive); // Step 2: Create presigned URL _uiMethods.DisplayTitle("Step 2: Create presigned POST URL"); var response = await CreatePresignedPostAsync(); _uiMethods.PressEnter(_isInteractive); // Step 3: Display URL and fields _uiMethods.DisplayTitle("Step 3: Presigned POST URL details"); DisplayPresignedPostFields(response); _uiMethods.PressEnter(_isInteractive); // Step 4: Upload file _uiMethods.DisplayTitle("Step 4: Upload test file using presigned POST URL"); await UploadFileAsync(response); _uiMethods.PressEnter(_isInteractive); // Step 5: Verify file exists await VerifyFileExistsAsync(); _uiMethods.PressEnter(_isInteractive); // Step 6: Cleanup _uiMethods.DisplayTitle("Step 6: Clean up resources"); await CleanupAsync(); _uiMethods.DisplayTitle("S3 Presigned POST Scenario completed successfully!"); _uiMethods.PressEnter(_isInteractive); } catch (Exception ex) { _logger.LogError(ex, "Error in scenario"); Console.WriteLine($"Error: {ex.Message}"); // Attempt cleanup if there was an error if (!string.IsNullOrEmpty(_bucketName)) { _uiMethods.DisplayTitle("Cleaning up resources after error"); await _s3Wrapper.DeleteBucketAsync(_bucketName); Console.WriteLine($"Cleaned up bucket: {_bucketName}"); } } } /// <summary> /// Create an S3 bucket for the scenario. /// </summary> private static async Task CreateBucketAsync() { _uiMethods.DisplayTitle("Step 1: Create an S3 bucket"); // Generate a default bucket name for the scenario var defaultBucketName = $"presigned-post-demo-{DateTime.Now:yyyyMMddHHmmss}".ToLower(); // Prompt user for bucket name or use default in non-interactive mode _bucketName = _uiMethods.GetUserInput( $"Enter S3 bucket name (or press Enter for '{defaultBucketName}'): ", defaultBucketName, _isInteractive); // Basic validation to ensure bucket name is not empty if (string.IsNullOrWhiteSpace(_bucketName)) { _bucketName = defaultBucketName; } Console.WriteLine($"Creating bucket: {_bucketName}"); await _s3Wrapper.CreateBucketAsync(_bucketName); Console.WriteLine($"Successfully created bucket: {_bucketName}"); } /// <summary> /// Create a presigned POST URL. /// </summary> private static async Task<CreatePresignedPostResponse> CreatePresignedPostAsync() { _objectKey = "example-upload.txt"; var expiration = DateTime.UtcNow.AddMinutes(10); // Short expiration for the demo Console.WriteLine($"Creating presigned POST URL for {_bucketName}/{_objectKey}"); Console.WriteLine($"Expiration: {expiration} UTC"); var s3Client = _s3Wrapper.GetS3Client(); var response = await _s3Wrapper.CreatePresignedPostAsync( s3Client, _bucketName!, _objectKey, expiration); Console.WriteLine("Successfully created presigned POST URL"); return response; } /// <summary> /// Upload a file using the presigned POST URL. /// </summary> private static async Task UploadFileAsync(CreatePresignedPostResponse response) { // Create a temporary test file to upload string testFilePath = Path.GetTempFileName(); string testContent = "This is a test file for the S3 presigned POST scenario."; await File.WriteAllTextAsync(testFilePath, testContent); Console.WriteLine($"Created test file at: {testFilePath}"); // Upload the file using the presigned POST URL Console.WriteLine("\nUploading file using the presigned POST URL..."); var uploadResult = await UploadFileWithPresignedPostAsync(response, testFilePath); // Display the upload result if (uploadResult.Success) { Console.WriteLine($"Upload successful! Status code: {uploadResult.StatusCode}"); } else { Console.WriteLine($"Upload failed with status code: {uploadResult.StatusCode}"); Console.WriteLine($"Error: {uploadResult.Response}"); throw new Exception("File upload failed"); } // Clean up the temporary file File.Delete(testFilePath); Console.WriteLine("Temporary file deleted"); } /// <summary> /// Helper method to upload a file using a presigned POST URL. /// </summary> private static async Task<(bool Success, HttpStatusCode StatusCode, string Response)> UploadFileWithPresignedPostAsync( CreatePresignedPostResponse response, string filePath) { try { _logger.LogInformation("Uploading file {filePath} using presigned POST URL", filePath); using var httpClient = _httpClientFactory.CreateClient(); using var formContent = new MultipartFormDataContent(); // Add all the fields from the presigned POST response foreach (var field in response.Fields) { formContent.Add(new StringContent(field.Value), field.Key); } // Add the file content var fileStream = File.OpenRead(filePath); var fileName = Path.GetFileName(filePath); var fileContent = new StreamContent(fileStream); fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); formContent.Add(fileContent, "file", fileName); // Send the POST request var httpResponse = await httpClient.PostAsync(response.Url, formContent); var responseContent = await httpResponse.Content.ReadAsStringAsync(); // Log and return the result _logger.LogInformation("Upload completed with status code {statusCode}", httpResponse.StatusCode); return (httpResponse.IsSuccessStatusCode, httpResponse.StatusCode, responseContent); } catch (Exception ex) { _logger.LogError(ex, "Error uploading file"); return (false, HttpStatusCode.InternalServerError, ex.Message); } } /// <summary> /// Verify that the uploaded file exists in the S3 bucket. /// </summary> private static async Task VerifyFileExistsAsync() { _uiMethods.DisplayTitle("Step 5: Verify uploaded file exists"); Console.WriteLine($"Checking if file exists at {_bucketName}/{_objectKey}..."); try { var metadata = await _s3Wrapper.GetObjectMetadataAsync(_bucketName!, _objectKey!); Console.WriteLine($"File verification successful! File exists in the bucket."); Console.WriteLine($"File size: {metadata.ContentLength} bytes"); Console.WriteLine($"File type: {metadata.Headers.ContentType}"); Console.WriteLine($"Last modified: {metadata.LastModified}"); } catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { Console.WriteLine($"Error: File was not found in the bucket."); throw; } } private static void DisplayPresignedPostFields(CreatePresignedPostResponse response) { Console.WriteLine($"Presigned POST URL: {response.Url}"); Console.WriteLine("Form fields to include:"); foreach (var field in response.Fields) { Console.WriteLine($" {field.Key}: {field.Value}"); } } /// <summary> /// Clean up resources created by the scenario. /// </summary> private static async Task CleanupAsync() { if (!string.IsNullOrEmpty(_bucketName)) { Console.WriteLine($"Deleting bucket {_bucketName} and its contents..."); bool result = await _s3Wrapper.DeleteBucketAsync(_bucketName); if (result) { Console.WriteLine("Bucket deleted successfully"); } else { Console.WriteLine("Failed to delete bucket - it may have been already deleted"); } } } }