버전 4(V4) AWS SDK for .NET 가 릴리스되었습니다.
변경 사항 해제 및 애플리케이션 마이그레이션에 대한 자세한 내용은 마이그레이션 주제를 참조하세요.
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SDK for .NET (v4)를 사용한 Amazon S3 예제
다음 코드 예제에서는 Amazon S3에서 AWS SDK for .NET (v4)를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접 호출하는 방법을 보여주며, 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 직접적으로 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.
시작하기
다음 코드 예제에서는 Amazon S3 사용을 시작하는 방법을 보여줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Hello Amazon Simple Storage Service // (Amazon S3) example. /// </summary> public class HelloS3 { /// <summary> /// Main method to run the Hello S3 example. /// </summary> /// <param name="args">Command line arguments.</param> /// <returns>A Task object.</returns> public static async Task Main(string[] args) { var s3Client = new AmazonS3Client(); try { Console.WriteLine("Hello Amazon S3! Let's list your buckets:"); Console.WriteLine(new string('-', 80)); // Use the built-in paginator to list buckets var request = new ListBucketsRequest(); var paginator = s3Client.Paginators.ListBuckets(request); var buckets = new List<S3Bucket>(); await foreach (var response in paginator.Responses) { buckets.AddRange(response.Buckets); } if (buckets.Any()) { Console.WriteLine($"Found {buckets.Count} S3 buckets:"); Console.WriteLine(); foreach (var bucket in buckets) { Console.WriteLine($"- Bucket Name: {bucket.BucketName}"); Console.WriteLine($" Creation Date: {bucket.CreationDate:yyyy-MM-dd HH:mm:ss UTC}"); Console.WriteLine(); } } else { Console.WriteLine("No S3 buckets found in your account."); } Console.WriteLine("Hello S3 completed successfully."); } catch (AmazonS3Exception ex) { Console.WriteLine($"S3 service error occurred: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"Couldn't list S3 buckets. Here's why: {ex.Message}"); } } }-
API 세부 정보는 AWS SDK for .NET API 참조의 ListBuckets를 참조하세요.
-
기본 사항
다음 코드 예제에서는 다음과 같은 작업을 수행하는 방법을 보여줍니다.
버킷을 만들고 버킷에 파일을 업로드합니다.
버킷에서 객체를 다운로드합니다.
버킷의 하위 폴더에 객체를 복사합니다.
버킷의 객체를 나열합니다.
버킷 객체와 버킷을 삭제합니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. Amazon S3 기능을 보여주는 대화형 시나리오를 실행합니다.
public class S3_Basics { public static bool IsInteractive = true; public static string BucketName = null!; public static string TempFilePath = null!; public static S3Wrapper _s3Wrapper = null!; public static ILogger<S3_Basics> _logger = null!; public static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonS3>() .AddTransient<S3Wrapper>() .AddLogging(builder => builder.AddConsole())) .Build(); _logger = LoggerFactory.Create(builder => builder.AddConsole()) .CreateLogger<S3_Basics>(); _s3Wrapper = host.Services.GetRequiredService<S3Wrapper>(); var sepBar = new string('-', 45); Console.WriteLine(sepBar); Console.WriteLine("Amazon Simple Storage Service (Amazon S3) basic"); Console.WriteLine("procedures. This application will:"); Console.WriteLine("\n\t1. Create a bucket"); Console.WriteLine("\n\t2. Upload an object to the new bucket"); Console.WriteLine("\n\t3. Copy the uploaded object to a folder in the bucket"); Console.WriteLine("\n\t4. List the items in the new bucket"); Console.WriteLine("\n\t5. Delete all the items in the bucket"); Console.WriteLine("\n\t6. Delete the bucket"); Console.WriteLine(sepBar); await RunScenario(_s3Wrapper, _logger); Console.WriteLine(sepBar); Console.WriteLine("The Amazon S3 scenario has successfully completed."); Console.WriteLine(sepBar); } /// <summary> /// Run the S3 Basics scenario with injected dependencies. /// </summary> /// <param name="s3Wrapper">The S3 wrapper instance.</param> /// <param name="scenarioLogger">The logger instance.</param> /// <returns>A Task object.</returns> public static async Task RunScenario(S3Wrapper s3Wrapper, ILogger<S3_Basics> scenarioLogger) { string bucketName = BucketName; string filePath = TempFilePath; string keyName = string.Empty; var sepBar = new string('-', 45); try { // Create a bucket. Console.WriteLine($"\n{sepBar}"); Console.WriteLine("\nCreate a new Amazon S3 bucket.\n"); Console.WriteLine(sepBar); if (IsInteractive) { Console.Write("Please enter a name for the new bucket: "); bucketName = Console.ReadLine(); } else { Console.WriteLine($"Using bucket name: {bucketName}"); } var success = await s3Wrapper.CreateBucketAsync(bucketName); if (success) { Console.WriteLine($"Successfully created bucket: {bucketName}.\n"); } else { Console.WriteLine($"Could not create bucket: {bucketName}.\n"); } Console.WriteLine(sepBar); Console.WriteLine("Upload a file to the new bucket."); Console.WriteLine(sepBar); if (IsInteractive) { // Get the local path and filename for the file to upload. while (string.IsNullOrEmpty(filePath)) { Console.Write("Please enter the path and filename of the file to upload: "); filePath = Console.ReadLine(); // Confirm that the file exists on the local computer. if (!File.Exists(filePath)) { Console.WriteLine($"Couldn't find {filePath}. Try again.\n"); filePath = string.Empty; } } } else { // Use the public variable if set, otherwise create a temp file if (!string.IsNullOrEmpty(TempFilePath)) { filePath = TempFilePath; Console.WriteLine($"Using provided test file: {filePath}"); } else { // Create a temporary test file for non-interactive mode filePath = Path.GetTempFileName(); var testContent = "This is a test file for S3 basics scenario.\nGenerated on: " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss UTC"); await File.WriteAllTextAsync(filePath, testContent); Console.WriteLine($"Created temporary test file: {filePath}"); } } // Get the file name from the full path. keyName = Path.GetFileName(filePath); success = await s3Wrapper.UploadFileAsync(bucketName, keyName, filePath); if (success) { Console.WriteLine($"Successfully uploaded {keyName} from {filePath} to {bucketName}.\n"); } else { Console.WriteLine($"Could not upload {keyName}.\n"); } // Set up download path string downloadPath = string.Empty; if (IsInteractive) { // Now get a new location where we can save the file. while (string.IsNullOrEmpty(downloadPath)) { // First get the path to which the file will be downloaded. Console.Write("Please enter the path where the file will be downloaded: "); downloadPath = Console.ReadLine(); // Confirm that the file doesn't already exist on the local computer. if (File.Exists($"{downloadPath}\\{keyName}")) { Console.WriteLine($"Sorry, the file already exists in that location.\n"); downloadPath = string.Empty; } } } else { downloadPath = Path.GetTempPath(); var downloadFile = Path.Combine(downloadPath, keyName); if (File.Exists(downloadFile)) { File.Delete(downloadFile); } Console.WriteLine($"Using download path: {downloadPath}"); } // Download an object from a bucket. success = await s3Wrapper.DownloadObjectFromBucketAsync(bucketName, keyName, downloadPath); if (success) { Console.WriteLine($"Successfully downloaded {keyName}.\n"); } else { Console.WriteLine($"Sorry, could not download {keyName}.\n"); } // Copy the object to a different folder in the bucket. string folderName = string.Empty; if (IsInteractive) { while (string.IsNullOrEmpty(folderName)) { Console.Write("Please enter the name of the folder to copy your object to: "); folderName = Console.ReadLine(); } } else { folderName = "test-folder"; Console.WriteLine($"Using folder name: {folderName}"); } await s3Wrapper.CopyObjectInBucketAsync(bucketName, keyName, folderName); // List the objects in the bucket. await s3Wrapper.ListBucketContentsAsync(bucketName); // Delete the contents of the bucket. if (IsInteractive) { Console.WriteLine("Press <Enter> when you are ready to delete the bucket contents."); _ = Console.ReadLine(); } var deleteContentsSuccess = await s3Wrapper.DeleteBucketContentsAsync(bucketName); if (deleteContentsSuccess) { Console.WriteLine($"Successfully deleted contents of {bucketName}.\n"); } else { Console.WriteLine($"Sorry, could not delete contents of {bucketName}.\n"); } if (IsInteractive) { // Deleting the bucket too quickly after separately deleting its contents can // cause an error that the bucket isn't empty. To delete contents and bucket in one // operation, use AmazonS3Util.DeleteS3BucketWithObjectsAsync Console.WriteLine("Press <Enter> when you are ready to delete the bucket."); _ = Console.ReadLine(); } else { // Add a small delay for non-interactive mode to ensure objects are fully deleted. Console.WriteLine("Waiting a moment for objects to be fully deleted..."); await Task.Delay(2000); } // Delete the bucket. var deleteSuccess = await s3Wrapper.DeleteBucketAsync(bucketName); if (deleteSuccess) { Console.WriteLine($"Successfully deleted {bucketName}.\n"); } else { Console.WriteLine($"Sorry, could not delete {bucketName}.\n"); } // Clean up temporary files in non-interactive mode if (!IsInteractive) { try { if (File.Exists(filePath)) { File.Delete(filePath); Console.WriteLine("Cleaned up temporary test file."); } var downloadFile = Path.Combine(downloadPath, keyName); if (File.Exists(downloadFile)) { File.Delete(downloadFile); Console.WriteLine("Cleaned up downloaded test file."); } } catch (Exception ex) { scenarioLogger.LogWarning(ex, "Failed to clean up temporary files."); } } } catch (Exception ex) { scenarioLogger.LogError(ex, "An error occurred during the S3 scenario execution."); // Clean up on error - delete bucket if it exists try { if (!string.IsNullOrEmpty(bucketName)) { await s3Wrapper.DeleteBucketContentsAsync(bucketName); await s3Wrapper.DeleteBucketAsync(bucketName); } } catch (Exception cleanupEx) { scenarioLogger.LogError(cleanupEx, "Error during cleanup."); } // Clean up temporary files in non-interactive mode if (!IsInteractive) { try { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { File.Delete(filePath); } } catch (Exception fileCleanupEx) { scenarioLogger.LogWarning(fileCleanupEx, "Failed to clean up temporary files during error handling."); } } throw; } } }Amazon S3 SDK 메서드의 래퍼 클래스입니다.
using Amazon.S3; using Amazon.S3.Model; namespace S3_Actions; /// <summary> /// This class contains all of the methods for working with Amazon Simple /// Storage Service (Amazon S3) buckets. /// </summary> public class S3Wrapper { private readonly IAmazonS3 _amazonS3; /// <summary> /// Initializes a new instance of the <see cref="S3Wrapper"/> class. /// </summary> /// <param name="amazonS3">An initialized Amazon S3 client object.</param> public S3Wrapper(IAmazonS3 amazonS3) { _amazonS3 = amazonS3; } /// <summary> /// Shows how to create a new Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket to create.</param> /// <returns>A boolean value representing the success or failure of /// the bucket creation process.</returns> public async Task<bool> CreateBucketAsync(string bucketName) { try { var request = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; var response = await _amazonS3.PutBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error creating bucket: '{ex.Message}'"); return false; } } /// <summary> /// Shows how to upload a file from the local computer to an Amazon S3 /// bucket. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to which the object /// will be uploaded.</param> /// <param name="objectName">The object to upload.</param> /// <param name="filePath">The path, including file name, of the object /// on the local computer to upload.</param> /// <returns>A boolean value indicating the success or failure of the /// upload procedure.</returns> public async Task<bool> UploadFileAsync( string bucketName, string objectName, string filePath) { try { var request = new PutObjectRequest { BucketName = bucketName, Key = objectName, FilePath = filePath, }; var response = await _amazonS3.PutObjectAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error uploading {objectName}: {ex.Message}"); return false; } } /// <summary> /// Shows how to download an object from an Amazon S3 bucket to the /// local computer. /// </summary> /// <param name="bucketName">The name of the bucket where the object is /// currently stored.</param> /// <param name="objectName">The name of the object to download.</param> /// <param name="filePath">The path, including filename, where the /// downloaded object will be stored.</param> /// <returns>A boolean value indicating the success or failure of the /// download process.</returns> public async Task<bool> DownloadObjectFromBucketAsync( string bucketName, string objectName, string filePath) { var request = new GetObjectRequest { BucketName = bucketName, Key = objectName, }; using GetObjectResponse response = await _amazonS3.GetObjectAsync(request); try { // Save object to local file await response.WriteResponseStreamToFileAsync($"{filePath}\\{objectName}", true, CancellationToken.None); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error saving {objectName}: {ex.Message}"); return false; } } /// <summary> /// Copies an object in an Amazon S3 bucket to a folder within the /// same bucket. /// </summary> /// <param name="bucketName">The name of the Amazon S3 bucket where the /// object to copy is located.</param> /// <param name="objectName">The object to be copied.</param> /// <param name="folderName">The folder to which the object will /// be copied.</param> /// <returns>A boolean value that indicates the success or failure of /// the copy operation.</returns> public async Task<bool> CopyObjectInBucketAsync( string bucketName, string objectName, string folderName) { try { var request = new CopyObjectRequest { SourceBucket = bucketName, SourceKey = objectName, DestinationBucket = bucketName, DestinationKey = $"{folderName}\\{objectName}", }; var response = await _amazonS3.CopyObjectAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error copying object: '{ex.Message}'"); return false; } } /// <summary> /// Shows how to list the objects in an Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket for which to list. /// <param name="printList">True to print out the list. /// <returns>The collection of objects.</returns> public async Task<List<S3Object>?> ListBucketContentsAsync(string bucketName, bool printList = true) { try { var request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 5, }; if (printList) { Console.WriteLine("--------------------------------------"); Console.WriteLine($"Listing the contents of {bucketName}:"); Console.WriteLine("--------------------------------------"); } var listObjectsV2Paginator = _amazonS3.Paginators.ListObjectsV2(new ListObjectsV2Request { BucketName = bucketName, }); var s3Objects = new List<S3Object>(); await foreach (var response in listObjectsV2Paginator.Responses) { if (response.S3Objects != null) { s3Objects.AddRange(response.S3Objects); } } if (printList) { Console.WriteLine($"Number of Objects: {s3Objects.Count}"); foreach (var entry in s3Objects) { Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}"); } } return s3Objects; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects."); return null; } } /// <summary> /// Delete all of the objects stored in an existing Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket from which the /// contents will be deleted.</param> /// <returns>A boolean value that represents the success or failure of /// deleting all of the objects in the bucket.</returns> public async Task<bool> DeleteBucketContentsAsync(string bucketName) { // Iterate over the contents of the bucket and delete all objects. try { // Delete all objects in the bucket. var deleteList = await ListBucketContentsAsync(bucketName, false); if (deleteList != null && deleteList.Any()) { await _amazonS3.DeleteObjectsAsync(new DeleteObjectsRequest() { BucketName = bucketName, Objects = deleteList.Select(o => new KeyVersion { Key = o.Key }).ToList(), }); } return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error deleting objects: {ex.Message}"); return false; } } /// <summary> /// Shows how to delete an Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the Amazon S3 bucket to delete.</param> /// <returns>A boolean value that represents the success or failure of /// the delete operation.</returns> public async Task<bool> DeleteBucketAsync(string bucketName) { try { var request = new DeleteBucketRequest { BucketName = bucketName, }; await _amazonS3.DeleteBucketAsync(request); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error deleting bucket: {ex.Message}"); return false; } } }-
API 세부 정보는 AWS SDK for .NET API 참조의 다음 항목을 참조하세요.
-
작업
다음 코드 예시는 CopyObject의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Copies an object in an Amazon S3 bucket to a folder within the /// same bucket. /// </summary> /// <param name="bucketName">The name of the Amazon S3 bucket where the /// object to copy is located.</param> /// <param name="objectName">The object to be copied.</param> /// <param name="folderName">The folder to which the object will /// be copied.</param> /// <returns>A boolean value that indicates the success or failure of /// the copy operation.</returns> public async Task<bool> CopyObjectInBucketAsync( string bucketName, string objectName, string folderName) { try { var request = new CopyObjectRequest { SourceBucket = bucketName, SourceKey = objectName, DestinationBucket = bucketName, DestinationKey = $"{folderName}\\{objectName}", }; var response = await _amazonS3.CopyObjectAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error copying object: '{ex.Message}'"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 CopyObject를 참조하세요.
-
다음 코드 예시는 CreateBucket의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Shows how to create a new Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket to create.</param> /// <returns>A boolean value representing the success or failure of /// the bucket creation process.</returns> public async Task<bool> CreateBucketAsync(string bucketName) { try { var request = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; var response = await _amazonS3.PutBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error creating bucket: '{ex.Message}'"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 CreateBucket을 참조하세요.
-
다음 코드 예시는 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); }-
API 세부 정보는 AWS SDK for .NET API 참조의 CreatePresignedPost를 참조하세요.
-
다음 코드 예시는 DeleteBucket의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Shows how to delete an Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the Amazon S3 bucket to delete.</param> /// <returns>A boolean value that represents the success or failure of /// the delete operation.</returns> public async Task<bool> DeleteBucketAsync(string bucketName) { try { var request = new DeleteBucketRequest { BucketName = bucketName, }; await _amazonS3.DeleteBucketAsync(request); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error deleting bucket: {ex.Message}"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 DeleteBucket을 참조하세요.
-
다음 코드 예시는 DeleteObjects의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Delete all of the objects stored in an existing Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket from which the /// contents will be deleted.</param> /// <returns>A boolean value that represents the success or failure of /// deleting all of the objects in the bucket.</returns> public async Task<bool> DeleteBucketContentsAsync(string bucketName) { // Iterate over the contents of the bucket and delete all objects. try { // Delete all objects in the bucket. var deleteList = await ListBucketContentsAsync(bucketName, false); if (deleteList != null && deleteList.Any()) { await _amazonS3.DeleteObjectsAsync(new DeleteObjectsRequest() { BucketName = bucketName, Objects = deleteList.Select(o => new KeyVersion { Key = o.Key }).ToList(), }); } return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error deleting objects: {ex.Message}"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 DeleteObjects를 참조하세요.
-
다음 코드 예시는 GetObject의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Shows how to download an object from an Amazon S3 bucket to the /// local computer. /// </summary> /// <param name="bucketName">The name of the bucket where the object is /// currently stored.</param> /// <param name="objectName">The name of the object to download.</param> /// <param name="filePath">The path, including filename, where the /// downloaded object will be stored.</param> /// <returns>A boolean value indicating the success or failure of the /// download process.</returns> public async Task<bool> DownloadObjectFromBucketAsync( string bucketName, string objectName, string filePath) { var request = new GetObjectRequest { BucketName = bucketName, Key = objectName, }; using GetObjectResponse response = await _amazonS3.GetObjectAsync(request); try { // Save object to local file await response.WriteResponseStreamToFileAsync($"{filePath}\\{objectName}", true, CancellationToken.None); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error saving {objectName}: {ex.Message}"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 GetObject를 참조하세요.
-
다음 코드 예시는 ListObjectsV2의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Shows how to list the objects in an Amazon S3 bucket. /// </summary> /// <param name="bucketName">The name of the bucket for which to list. /// <param name="printList">True to print out the list. /// <returns>The collection of objects.</returns> public async Task<List<S3Object>?> ListBucketContentsAsync(string bucketName, bool printList = true) { try { var request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 5, }; if (printList) { Console.WriteLine("--------------------------------------"); Console.WriteLine($"Listing the contents of {bucketName}:"); Console.WriteLine("--------------------------------------"); } var listObjectsV2Paginator = _amazonS3.Paginators.ListObjectsV2(new ListObjectsV2Request { BucketName = bucketName, }); var s3Objects = new List<S3Object>(); await foreach (var response in listObjectsV2Paginator.Responses) { if (response.S3Objects != null) { s3Objects.AddRange(response.S3Objects); } } if (printList) { Console.WriteLine($"Number of Objects: {s3Objects.Count}"); foreach (var entry in s3Objects) { Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}"); } } return s3Objects; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects."); return null; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 ListObjectsV2를 참조하세요.
-
다음 코드 예시는 PutObject의 사용 방법을 보여 줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. /// <summary> /// Shows how to upload a file from the local computer to an Amazon S3 /// bucket. /// </summary> /// <param name="bucketName">The Amazon S3 bucket to which the object /// will be uploaded.</param> /// <param name="objectName">The object to upload.</param> /// <param name="filePath">The path, including file name, of the object /// on the local computer to upload.</param> /// <returns>A boolean value indicating the success or failure of the /// upload procedure.</returns> public async Task<bool> UploadFileAsync( string bucketName, string objectName, string filePath) { try { var request = new PutObjectRequest { BucketName = bucketName, Key = objectName, FilePath = filePath, }; var response = await _amazonS3.PutObjectAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error uploading {objectName}: {ex.Message}"); return false; } }-
API 세부 정보는 AWS SDK for .NET API 참조의 PutObject를 참조하십시오.
-
시나리오
다음 코드 예제는 Amazon S3에 대해 미리 서명된 URL을 생성하고 객체를 업로드하는 방법을 보여줍니다.
- SDK for .NET (v4)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예 리포지토리
에서 전체 예를 찾고 설정 및 실행하는 방법을 배워보세요. 브라우저에서 직접 업로드할 수 있도록 미리 서명된 POST URL을 생성하고 사용합니다.
/// <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"); } } } }