Ejemplos de Amazon S3 con SDK for .NET (v4) - AWS SDK for .NET (V4)

¡Se AWS SDK for .NET ha publicado la versión 4 (V4) del!

Para obtener información sobre los cambios más importantes y la migración de sus aplicaciones, consulte el tema sobre migración.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Ejemplos de Amazon S3 con SDK for .NET (v4)

Los siguientes ejemplos de código muestran cómo realizar acciones e implementar situaciones comunes mediante el uso de la versión AWS SDK for .NET 4 con Amazon S3.

Los conceptos básicos son ejemplos de código que muestran cómo realizar las operaciones esenciales dentro de un servicio.

Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las distintas funciones de servicio, es posible ver las acciones en contexto en los escenarios relacionados.

Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica a través de llamadas a varias funciones dentro del servicio o combinado con otros Servicios de AWS.

En cada ejemplo se incluye un enlace al código de origen completo, con instrucciones de configuración y ejecución del código en el contexto.

Introducción

El siguiente ejemplo de código muestra cómo empezar a utilizar Amazon S3.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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}"); } } }
  • Para obtener más información sobre la API, consulta ListBucketsla Referencia AWS SDK for .NET de la API.

Conceptos básicos

En el siguiente ejemplo de código, se muestra cómo:

  • Creación de un bucket y cargar un archivo en el bucket.

  • Descargar un objeto desde un bucket.

  • Copiar un objeto en una subcarpeta de un bucket.

  • Obtención de una lista de los objetos de un bucket.

  • Eliminación del bucket y todos los objetos que incluye.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Ejecutar un escenario interactivo en el que se muestran las características de 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; } } }

Una clase contenedora para métodos del SDK de Amazon S3.

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; } } }

Acciones

En el siguiente ejemplo de código, se muestra cómo utilizar CopyObject.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta CopyObjectla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar CreateBucket.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta CreateBucketla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar CreatePresignedPost.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Crear una URL POST previamente firmada.

/// <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); }
  • Para obtener más información sobre la API, consulta CreatePresignedPostla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteBucket.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta DeleteBucketla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteObjects.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta DeleteObjectsla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar GetObject.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta GetObjectla Referencia AWS SDK for .NET de la API.

En el siguiente ejemplo de código, se muestra cómo utilizar ListObjectsV2.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta la ListObjectsversión 2 en la referencia de la AWS SDK for .NET API.

En el siguiente ejemplo de código, se muestra cómo utilizar PutObject.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para obtener más información sobre la API, consulta PutObjectla Referencia AWS SDK for .NET de la API.

Escenarios

En el siguiente ejemplo de código se muestra cómo crear una URL prefirmada para Amazon S3 y cargar un objeto.

SDK for .NET (v4)
nota

Hay más información GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Crea y usa un POST prefirmado URLs para subirlo directamente al navegador.

/// <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"); } } } }