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) を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

「シナリオ」は、1 つのサービス内から、または他の 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); }
  • API の詳細については、AWS SDK for .NET 「 API リファレンス」のCreatePresignedPost」を参照してください。

シナリオ

次のコード例は、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"); } } } }