La version 4 (V4) du AWS SDK pour .NET est sortie !
Pour plus d'informations sur les modifications majeures et la migration de vos applications, consultez la rubrique relative à la migration.
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Exemples d'Amazon S3 utilisant SDK pour .NET (v4)
Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants en utilisant le AWS SDK pour .NET (v4) avec Amazon S3.
Les principes de base sont des exemples de code qui vous montrent comment effectuer les opérations essentielles au sein d’un service.
Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Alors que les actions vous indiquent comment appeler des fonctions de service individuelles, vous pouvez les voir en contexte dans leurs scénarios associés.
Les scénarios sont des exemples de code qui vous montrent comment accomplir des tâches spécifiques en appelant plusieurs fonctions au sein d’un même service ou combinés à d’autres Services AWS.
Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la configuration et l’exécution du code en contexte.
Mise en route
L'exemple de code suivant montre comment commencer à utiliser Amazon S3.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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}"); } } }-
Pour plus de détails sur l'API, reportez-vous ListBucketsà la section Référence des AWS SDK pour .NET API.
-
Principes de base
L’exemple de code suivant illustre comment :
créer un compartiment et y charger un fichier ;
télécharger un objet à partir d’un compartiment ;
copier un objet dans le sous-dossier d’un compartiment ;
répertorier les objets d’un compartiment ;
supprimer le compartiment et tous les objets qui y figurent.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Exécutez un scénario interactif illustrant les fonctionnalités d’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; } } }Une classe d’encapsuleur pour les méthodes du kit SDK 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; } } }-
Pour plus de détails sur l’API, consultez les rubriques suivantes dans la Référence des API du kit AWS SDK pour .NET .
-
Actions
L'exemple de code suivant montre comment utiliserCopyObject.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous CopyObjectà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserCreateBucket.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous CreateBucketà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserCreatePresignedPost.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Créez une URL POST présignée.
/// <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); }-
Pour plus de détails sur l'API, reportez-vous CreatePresignedPostà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserDeleteBucket.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous DeleteBucketà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserDeleteObjects.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous DeleteObjectsà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserGetObject.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous GetObjectà la section Référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserListObjectsV2.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, voir ListObjectsV2 dans le manuel de référence des AWS SDK pour .NET API.
-
L'exemple de code suivant montre comment utiliserPutObject.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code 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; } }-
Pour plus de détails sur l'API, reportez-vous PutObjectà la section Référence des AWS SDK pour .NET API.
-
Scénarios
L’exemple de code suivant montre comment créer une URL présignée pour Amazon S3 et charger un objet.
- SDK pour .NET (v4)
-
Note
Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Créez et utilisez un POST présigné URLs pour les téléchargements directs par navigateur.
/// <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"); } } } }