Amazon S3 examples using Amazon SDK for .NET - Amazon SDK for .NET
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Amazon S3 examples using Amazon SDK for .NET

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for .NET with Amazon S3.

Actions are code excerpts that show you how to call individual service functions.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Actions

The following code example shows how to add cross-origin resource sharing (CORS) rules to an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Add CORS configuration to the Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used /// to apply the CORS configuration to an Amazon S3 bucket.</param> /// <param name="configuration">The CORS configuration to apply.</param> private static async Task PutCORSConfigurationAsync(AmazonS3Client client, CORSConfiguration configuration) { PutCORSConfigurationRequest request = new() { BucketName = BucketName, Configuration = configuration, }; _ = await client.PutCORSConfigurationAsync(request); }
  • For API details, see PutBucketCors in Amazon SDK for .NET API Reference.

The following code example shows how to add a lifecycle configuration to an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Adds lifecycle configuration information to the S3 bucket named in /// the bucketName parameter. /// </summary> /// <param name="client">The S3 client used to call the /// PutLifecycleConfigurationAsync method.</param> /// <param name="bucketName">A string representing the S3 bucket to /// which configuration information will be added.</param> /// <param name="configuration">A LifecycleConfiguration object that /// will be applied to the S3 bucket.</param> public static async Task AddExampleLifecycleConfigAsync(IAmazonS3 client, string bucketName, LifecycleConfiguration configuration) { var request = new PutLifecycleConfigurationRequest() { BucketName = bucketName, Configuration = configuration, }; var response = await client.PutLifecycleConfigurationAsync(request); }

The following code example shows how to cancel multipart uploads.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; public class AbortMPU { public static async Task Main() { string bucketName = "doc-example-bucket"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the S3 client object's constructor. // For example: RegionEndpoint.USWest2. IAmazonS3 client = new AmazonS3Client(); await AbortMPUAsync(client, bucketName); } /// <summary> /// Cancels the multi-part copy process. /// </summary> /// <param name="client">The initialized client object used to create /// the TransferUtility object.</param> /// <param name="bucketName">The name of the S3 bucket where the /// multi-part copy operation is in progress.</param> public static async Task AbortMPUAsync(IAmazonS3 client, string bucketName) { try { var transferUtility = new TransferUtility(client); // Cancel all in-progress uploads initiated before the specified date. await transferUtility.AbortMultipartUploadsAsync( bucketName, DateTime.Now.AddDays(-7)); } catch (AmazonS3Exception e) { Console.WriteLine($"Error: {e.Message}"); } } }

The following code example shows how to copy an S3 object from one bucket to another.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class CopyObject { public static async Task Main() { // Specify the AWS Region where your buckets are located if it is // different from the AWS Region of the default user. IAmazonS3 s3Client = new AmazonS3Client(); // Remember to change these values to refer to your Amazon S3 objects. string sourceBucketName = "doc-example-bucket1"; string destinationBucketName = "doc-example-bucket2"; string sourceObjectKey = "testfile.txt"; string destinationObjectKey = "testfilecopy.txt"; Console.WriteLine($"Copying {sourceObjectKey} from {sourceBucketName} to "); Console.WriteLine($"{destinationBucketName} as {destinationObjectKey}"); var response = await CopyingObjectAsync( s3Client, sourceObjectKey, destinationObjectKey, sourceBucketName, destinationBucketName); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine("\nCopy complete."); } } /// <summary> /// This method calls the AWS SDK for .NET to copy an /// object from one Amazon S3 bucket to another. /// </summary> /// <param name="client">The Amazon S3 client object.</param> /// <param name="sourceKey">The name of the object to be copied.</param> /// <param name="destinationKey">The name under which to save the copy.</param> /// <param name="sourceBucketName">The name of the Amazon S3 bucket /// where the file is located now.</param> /// <param name="destinationBucketName">The name of the Amazon S3 /// bucket where the copy should be saved.</param> /// <returns>Returns a CopyObjectResponse object with the results from /// the async call.</returns> public static async Task<CopyObjectResponse> CopyingObjectAsync( IAmazonS3 client, string sourceKey, string destinationKey, string sourceBucketName, string destinationBucketName) { var response = new CopyObjectResponse(); try { var request = new CopyObjectRequest { SourceBucket = sourceBucketName, SourceKey = sourceKey, DestinationBucket = destinationBucketName, DestinationKey = destinationKey, }; response = await client.CopyObjectAsync(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error copying object: '{ex.Message}'"); } return response; } }
  • For API details, see CopyObject in Amazon SDK for .NET API Reference.

The following code example shows how to create an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Shows how to create a new Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <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 static async Task<bool> CreateBucketAsync(IAmazonS3 client, string bucketName) { try { var request = new PutBucketRequest { BucketName = bucketName, UseClientRegion = true, }; var response = await client.PutBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error creating bucket: '{ex.Message}'"); return false; } }
  • For API details, see CreateBucket in Amazon SDK for .NET API Reference.

The following code example shows how to delete CORS rules from an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Deletes a CORS configuration from an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used /// to delete the CORS configuration from the bucket.</param> private static async Task DeleteCORSConfigurationAsync(AmazonS3Client client) { DeleteCORSConfigurationRequest request = new() { BucketName = BucketName, }; await client.DeleteCORSConfigurationAsync(request); }

The following code example shows how to delete an empty S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Shows how to delete an Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <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 static async Task<bool> DeleteBucketAsync(IAmazonS3 client, string bucketName) { var request = new DeleteBucketRequest { BucketName = bucketName, }; var response = await client.DeleteBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
  • For API details, see DeleteBucket in Amazon SDK for .NET API Reference.

The following code example shows how to delete an S3 object.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete an object in a non-versioned S3 bucket.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class DeleteObject { /// <summary> /// The Main method initializes the necessary variables and then calls /// the DeleteObjectNonVersionedBucketAsync method to delete the object /// named by the keyName parameter. /// </summary> public static async Task Main() { const string bucketName = "doc-example-bucket"; const string keyName = "testfile.txt"; // If the Amazon S3 bucket is located in an AWS Region other than the // Region of the default account, define the AWS Region for the // Amazon S3 bucket in your call to the AmazonS3Client constructor. // For example RegionEndpoint.USWest2. IAmazonS3 client = new AmazonS3Client(); await DeleteObjectNonVersionedBucketAsync(client, bucketName, keyName); } /// <summary> /// The DeleteObjectNonVersionedBucketAsync takes care of deleting the /// desired object from the named bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client used to delete /// an object from an Amazon S3 bucket.</param> /// <param name="bucketName">The name of the bucket from which the /// object will be deleted.</param> /// <param name="keyName">The name of the object to delete.</param> public static async Task DeleteObjectNonVersionedBucketAsync(IAmazonS3 client, string bucketName, string keyName) { try { var deleteObjectRequest = new DeleteObjectRequest { BucketName = bucketName, Key = keyName, }; Console.WriteLine($"Deleting object: {keyName}"); await client.DeleteObjectAsync(deleteObjectRequest); Console.WriteLine($"Object: {keyName} deleted from {bucketName}."); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' when deleting an object."); } } }

Delete an object in a versioned S3 bucket.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class DeleteObjectVersion { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "verstioned-object.txt"; // If the AWS Region of the default user is different from the AWS // Region of the Amazon S3 bucket, pass the AWS Region of the // bucket region to the Amazon S3 client object's constructor. // Define it like this: // RegionEndpoint bucketRegion = RegionEndpoint.USWest2; IAmazonS3 client = new AmazonS3Client(); await CreateAndDeleteObjectVersionAsync(client, bucketName, keyName); } /// <summary> /// This method creates and then deletes a versioned object. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to /// create and delete the object.</param> /// <param name="bucketName">The name of the Amazon S3 bucket where the /// object will be created and deleted.</param> /// <param name="keyName">The key name of the object to create.</param> public static async Task CreateAndDeleteObjectVersionAsync(IAmazonS3 client, string bucketName, string keyName) { try { // Add a sample object. string versionID = await PutAnObject(client, bucketName, keyName); // Delete the object by specifying an object key and a version ID. DeleteObjectRequest request = new() { BucketName = bucketName, Key = keyName, VersionId = versionID, }; Console.WriteLine("Deleting an object"); await client.DeleteObjectAsync(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } /// <summary> /// This method is used to create the temporary Amazon S3 object. /// </summary> /// <param name="client">The initialized Amazon S3 object which will be used /// to create the temporary Amazon S3 object.</param> /// <param name="bucketName">The name of the Amazon S3 bucket where the object /// will be created.</param> /// <param name="objectKey">The name of the Amazon S3 object co create.</param> /// <returns>The Version ID of the created object.</returns> public static async Task<string> PutAnObject(IAmazonS3 client, string bucketName, string objectKey) { PutObjectRequest request = new() { BucketName = bucketName, Key = objectKey, ContentBody = "This is the content body!", }; PutObjectResponse response = await client.PutObjectAsync(request); return response.VersionId; } }
  • For API details, see DeleteObject in Amazon SDK for .NET API Reference.

The following code example shows how to delete multiple objects from an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Delete all objects in an S3 bucket.

/// <summary> /// Delete all of the objects stored in an existing Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <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 static async Task<bool> DeleteBucketContentsAsync(IAmazonS3 client, string bucketName) { // Iterate over the contents of the bucket and delete all objects. var request = new ListObjectsV2Request { BucketName = bucketName, }; try { ListObjectsV2Response response; do { response = await client.ListObjectsV2Async(request); response.S3Objects .ForEach(async obj => await client.DeleteObjectAsync(bucketName, obj.Key)); // If the response is truncated, set the request ContinuationToken // from the NextContinuationToken property of the response. request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error deleting objects: {ex.Message}"); return false; } }

Delete multiple objects in a non-versioned S3 bucket.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class DeleteMultipleObjects { /// <summary> /// The Main method initializes the Amazon S3 client and the name of /// the bucket and then passes those values to MultiObjectDeleteAsync. /// </summary> public static async Task Main() { const string bucketName = "doc-example-bucket"; // If the Amazon S3 bucket from which you wish to delete objects is not // located in the same AWS Region as the default user, define the // AWS Region for the Amazon S3 bucket as a parameter to the client // constructor. IAmazonS3 s3Client = new AmazonS3Client(); await MultiObjectDeleteAsync(s3Client, bucketName); } /// <summary> /// This method uses the passed Amazon S3 client to first create and then /// delete three files from the named bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// Amazon S3 methods.</param> /// <param name="bucketName">The name of the Amazon S3 bucket where objects /// will be created and then deleted.</param> public static async Task MultiObjectDeleteAsync(IAmazonS3 client, string bucketName) { // Create three sample objects which we will then delete. var keysAndVersions = await PutObjectsAsync(client, 3, bucketName); // Now perform the multi-object delete, passing the key names and // version IDs. Since we are working with a non-versioned bucket, // the object keys collection includes null version IDs. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keysAndVersions, }; // You can add a specific object key to the delete request using the // AddKey method of the multiObjectDeleteRequest. try { DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException e) { PrintDeletionErrorStatus(e); } } /// <summary> /// Prints the list of errors raised by the call to DeleteObjectsAsync. /// </summary> /// <param name="ex">A collection of exceptions returned by the call to /// DeleteObjectsAsync.</param> public static void PrintDeletionErrorStatus(DeleteObjectsException ex) { DeleteObjectsResponse errorResponse = ex.Response; Console.WriteLine("x {0}", errorResponse.DeletedObjects.Count); Console.WriteLine($"Successfully deleted {errorResponse.DeletedObjects.Count}."); Console.WriteLine($"No. of objects failed to delete = {errorResponse.DeleteErrors.Count}"); Console.WriteLine("Printing error data..."); foreach (DeleteError deleteError in errorResponse.DeleteErrors) { Console.WriteLine($"Object Key: {deleteError.Key}\t{deleteError.Code}\t{deleteError.Message}"); } } /// <summary> /// This method creates simple text file objects that can be used in /// the delete method. /// </summary> /// <param name="client">The Amazon S3 client used to call PutObjectAsync.</param> /// <param name="number">The number of objects to create.</param> /// <param name="bucketName">The name of the bucket where the objects /// will be created.</param> /// <returns>A list of keys (object keys) and versions that the calling /// method will use to delete the newly created files.</returns> public static async Task<List<KeyVersion>> PutObjectsAsync(IAmazonS3 client, int number, string bucketName) { List<KeyVersion> keys = new List<KeyVersion>(); for (int i = 0; i < number; i++) { string key = "ExampleObject-" + new System.Random().Next(); PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = "This is the content body!", }; PutObjectResponse response = await client.PutObjectAsync(request); // For non-versioned bucket operations, we only need the // object key. KeyVersion keyVersion = new KeyVersion { Key = key, }; keys.Add(keyVersion); } return keys; } }

Delete multiple objects in a versioned S3 bucket.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class DeleteMultipleObjects { public static async Task Main() { string bucketName = "doc-example-bucket"; // If the AWS Region for your Amazon S3 bucket is different from // the AWS Region of the default user, define the AWS Region for // the Amazon S3 bucket and pass it to the client constructor // like this: // RegionEndpoint bucketRegion = RegionEndpoint.USWest2; IAmazonS3 s3Client; s3Client = new AmazonS3Client(); await DeleteMultipleObjectsFromVersionedBucketAsync(s3Client, bucketName); } /// <summary> /// This method removes multiple versions and objects from a /// version-enabled Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and /// RemoveDeleteMarkersAsync.</param> /// <param name="bucketName">The name of the bucket from which to delete /// objects.</param> public static async Task DeleteMultipleObjectsFromVersionedBucketAsync(IAmazonS3 client, string bucketName) { // Delete objects (specifying object version in the request). await DeleteObjectVersionsAsync(client, bucketName); // Delete objects (without specifying object version in the request). var deletedObjects = await DeleteObjectsAsync(client, bucketName); // Additional exercise - remove the delete markers Amazon S3 returned from // the preceding response. This results in the objects reappearing // in the bucket (you can verify the appearance/disappearance of // objects in the console). await RemoveDeleteMarkersAsync(client, bucketName, deletedObjects); } /// <summary> /// Creates and then deletes non-versioned Amazon S3 objects and then deletes /// them again. The method returns a list of the Amazon S3 objects deleted. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// PubObjectsAsync and NonVersionedDeleteAsync.</param> /// <param name="bucketName">The name of the bucket where the objects /// will be created and then deleted.</param> /// <returns>A list of DeletedObjects.</returns> public static async Task<List<DeletedObject>> DeleteObjectsAsync(IAmazonS3 client, string bucketName) { // Upload the sample objects. var keysAndVersions2 = await PutObjectsAsync(client, bucketName, 3); // Delete objects using only keys. Amazon S3 creates a delete marker and // returns its version ID in the response. List<DeletedObject> deletedObjects = await NonVersionedDeleteAsync(client, bucketName, keysAndVersions2); return deletedObjects; } /// <summary> /// This method creates several temporary objects and then deletes them. /// </summary> public static async Task DeleteObjectVersionsAsync(IAmazonS3 client, string bucketName) { // Upload the sample objects. var keysAndVersions1 = await PutObjectsAsync(client, bucketName, 3); // Delete the specific object versions. await VersionedDeleteAsync(client, bucketName, keysAndVersions1); } /// <summary> /// Displays the list of information about deleted files to the console. /// </summary> /// <param name="e">Error information from the delete process.</param> private static void DisplayDeletionErrors(DeleteObjectsException e) { var errorResponse = e.Response; Console.WriteLine($"No. of objects successfully deleted = {errorResponse.DeletedObjects.Count}"); Console.WriteLine($"No. of objects failed to delete = {errorResponse.DeleteErrors.Count}"); Console.WriteLine("Printing error data..."); foreach (var deleteError in errorResponse.DeleteErrors) { Console.WriteLine($"Object Key: {deleteError.Key}\t{deleteError.Code}\t{deleteError.Message}"); } } /// <summary> /// Delete multiple objects from a version-enabled bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and /// RemoveDeleteMarkersAsync.</param> /// <param name="bucketName">The name of the bucket from which to delete /// objects.</param> /// <param name="keys">A list of key names for the objects to delete.</param> static async Task VersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys) { var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keys, // This includes the object keys and specific version IDs. }; try { Console.WriteLine("Executing VersionedDelete..."); DeleteObjectsResponse response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine($"Successfully deleted all the {response.DeletedObjects.Count} items"); } catch (DeleteObjectsException ex) { DisplayDeletionErrors(ex); } } /// <summary> /// Deletes multiple objects from a non-versioned Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and /// RemoveDeleteMarkersAsync.</param> /// <param name="bucketName">The name of the bucket from which to delete /// objects.</param> /// <param name="keys">A list of key names for the objects to delete.</param> /// <returns>A list of the deleted objects.</returns> static async Task<List<DeletedObject>> NonVersionedDeleteAsync(IAmazonS3 client, string bucketName, List<KeyVersion> keys) { // Create a request that includes only the object key names. DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(); multiObjectDeleteRequest.BucketName = bucketName; foreach (var key in keys) { multiObjectDeleteRequest.AddKey(key.Key); } // Execute DeleteObjectsAsync. // The DeleteObjectsAsync method adds a delete marker for each // object deleted. You can verify that the objects were removed // using the Amazon S3 console. DeleteObjectsResponse response; try { Console.WriteLine("Executing NonVersionedDelete..."); response = await client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine("Successfully deleted all the {0} items", response.DeletedObjects.Count); } catch (DeleteObjectsException ex) { DisplayDeletionErrors(ex); throw; // Some deletions failed. Investigate before continuing. } // This response contains the DeletedObjects list which we use to delete the delete markers. return response.DeletedObjects; } /// <summary> /// Deletes the markers left after deleting the temporary objects. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// DeleteObjectVersionsAsync, DeleteObjectsAsync, and /// RemoveDeleteMarkersAsync.</param> /// <param name="bucketName">The name of the bucket from which to delete /// objects.</param> /// <param name="deletedObjects">A list of the objects that were deleted.</param> private static async Task RemoveDeleteMarkersAsync(IAmazonS3 client, string bucketName, List<DeletedObject> deletedObjects) { var keyVersionList = new List<KeyVersion>(); foreach (var deletedObject in deletedObjects) { KeyVersion keyVersion = new KeyVersion { Key = deletedObject.Key, VersionId = deletedObject.DeleteMarkerVersionId, }; keyVersionList.Add(keyVersion); } // Create another request to delete the delete markers. var multiObjectDeleteRequest = new DeleteObjectsRequest { BucketName = bucketName, Objects = keyVersionList, }; // Now, delete the delete marker to bring your objects back to the bucket. try { Console.WriteLine("Removing the delete markers ....."); var deleteObjectResponse = await client.DeleteObjectsAsync(multiObjectDeleteRequest); Console.WriteLine($"Successfully deleted the {deleteObjectResponse.DeletedObjects.Count} delete markers"); } catch (DeleteObjectsException ex) { DisplayDeletionErrors(ex); } } /// <summary> /// Create temporary Amazon S3 objects to show how object deletion wors in an /// Amazon S3 bucket with versioning enabled. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// PutObjectAsync to create temporary objects for the example.</param> /// <param name="bucketName">A string representing the name of the S3 /// bucket where we will create the temporary objects.</param> /// <param name="number">The number of temporary objects to create.</param> /// <returns>A list of the KeyVersion objects.</returns> static async Task<List<KeyVersion>> PutObjectsAsync(IAmazonS3 client, string bucketName, int number) { var keys = new List<KeyVersion>(); for (var i = 0; i < number; i++) { string key = "ObjectToDelete-" + new System.Random().Next(); PutObjectRequest request = new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = "This is the content body!", }; var response = await client.PutObjectAsync(request); KeyVersion keyVersion = new KeyVersion { Key = key, VersionId = response.VersionId, }; keys.Add(keyVersion); } return keys; } }
  • For API details, see DeleteObjects in Amazon SDK for .NET API Reference.

The following code example shows how to delete the lifecycle configuration of an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// This method removes the Lifecycle configuration from the named /// S3 bucket. /// </summary> /// <param name="client">The S3 client object used to call /// the RemoveLifecycleConfigAsync method.</param> /// <param name="bucketName">A string representing the name of the /// S3 bucket from which the configuration will be removed.</param> public static async Task RemoveLifecycleConfigAsync(IAmazonS3 client, string bucketName) { var request = new DeleteLifecycleConfigurationRequest() { BucketName = bucketName, }; await client.DeleteLifecycleConfigurationAsync(request); }

The following code example shows how to enable logging on an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class ServerAccessLogging { public static async Task Main() { string bucketName = "doc-example-bucket"; string logBucketName = "doc-example-logs-bucket"; string logObjectKeyPrefix = "Logs"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USWest2 or RegionEndpoint.USEast2. IAmazonS3 client = new AmazonS3Client(); try { // Grant Log Delivery group permission to write log entries // to the target bucket. await GrantPermissionsToWriteLogsAsync(client, logBucketName); // Enable logging on the source bucket. await EnableLoggingAsync(client, bucketName, logBucketName, logObjectKeyPrefix); } catch (AmazonS3Exception e) { Console.WriteLine($"Error: {e.Message}"); } } /// <summary> /// This method grants appropriate permissions for logging to the /// Amazon S3 bucket where the logs will be stored. /// </summary> /// <param name="client">The initialized Amazon S3 client which will be used /// to create and apply an ACL for the logging bucket.</param> /// <param name="logBucketName">The name of the bucket where logging /// information will be stored.</param> public static async Task GrantPermissionsToWriteLogsAsync(IAmazonS3 client, string logBucketName) { var aclResponse = await client.GetACLAsync(new GetACLRequest { BucketName = logBucketName }); var bucketACL = aclResponse.AccessControlList; bucketACL.AddGrant(new S3Grantee { URI = "http://acs.amazonaws.com/groups/s3/LogDelivery" }, S3Permission.WRITE); bucketACL.AddGrant(new S3Grantee { URI = "http://acs.amazonaws.com/groups/s3/LogDelivery" }, S3Permission.READ_ACP); var setACLRequest = new PutACLRequest { AccessControlList = bucketACL, BucketName = logBucketName, }; await client.PutACLAsync(setACLRequest); } /// <summary> /// This method enables logging for an Amazon S3 bucket. Logs will be stored /// in the bucket you selected for logging. Each the logObjectKeyPrefix /// will be prepended to each log oject. /// </summary> /// <param name="client">The initialized Amazon S3 client which will be used /// to configure and apply logging to the selected Amazon S3 bucket.</param> /// <param name="bucketName">The name of the Amazon S3 bucket for which you /// wish to enable logging.</param> /// <param name="logBucketName">The name of the Amazon S3 bucket where logging /// information will be stored.</param> /// <param name="logObjectKeyPrefix">The prefix to prepend to each /// object key</param> public static async Task EnableLoggingAsync( IAmazonS3 client, string bucketName, string logBucketName, string logObjectKeyPrefix) { var loggingConfig = new S3BucketLoggingConfig { TargetBucketName = logBucketName, TargetPrefix = logObjectKeyPrefix, }; var putBucketLoggingRequest = new PutBucketLoggingRequest { BucketName = bucketName, LoggingConfig = loggingConfig, }; await client.PutBucketLoggingAsync(putBucketLoggingRequest); } }

The following code example shows how to enable notifications for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class EnableNotifications { public static async Task Main() { const string bucketName = "doc-example-bucket1"; const string snsTopic = "arn:aws:sns:us-east-2:0123456789ab:bucket-notify"; const string sqsQueue = "arn:aws:sqs:us-east-2:0123456789ab:Example_Queue"; IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2); await EnableNotificationAsync(client, bucketName, snsTopic, sqsQueue); } /// <summary> /// This method makes the call to the PutBucketNotificationAsync method. /// </summary> /// <param name="client">An initialized Amazon S3 client used to call /// the PutBucketNotificationAsync method.</param> /// <param name="bucketName">The name of the bucket for which /// notifications will be turned on.</param> /// <param name="snsTopic">The ARN for the Amazon Simple Notification /// Service (Amazon SNS) topic associated with the S3 bucket.</param> /// <param name="sqsQueue">The ARN of the Amazon Simple Queue Service /// (Amazon SQS) queue to which notifications will be pushed.</param> public static async Task EnableNotificationAsync( IAmazonS3 client, string bucketName, string snsTopic, string sqsQueue) { try { // The bucket for which we are setting up notifications. var request = new PutBucketNotificationRequest() { BucketName = bucketName, }; // Defines the topic to use when sending a notification. var topicConfig = new TopicConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedCopy }, Topic = snsTopic, }; request.TopicConfigurations = new List<TopicConfiguration> { topicConfig, }; request.QueueConfigurations = new List<QueueConfiguration> { new QueueConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedPut }, Queue = sqsQueue, }, }; // Now apply the notification settings to the bucket. PutBucketNotificationResponse response = await client.PutBucketNotificationAsync(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }

The following code example shows how to enable transfer acceleration for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; class TransferAcceleration { /// <summary> /// The main method initializes the client object and sets the /// Amazon Simple Storage Service (Amazon S3) bucket name before /// calling EnableAccelerationAsync. /// </summary> public static async Task Main() { var s3Client = new AmazonS3Client(); const string bucketName = "doc-example-bucket"; await EnableAccelerationAsync(s3Client, bucketName); } /// <summary> /// This method sets the configuration to enable transfer acceleration /// for the bucket referred to in the bucketName parameter. /// </summary> /// <param name="client">An Amazon S3 client used to enable the /// acceleration on an Amazon S3 bucket.</param> /// <param name="bucketName">The name of the Amazon S3 bucket for which the /// method will be enabling acceleration.</param> static async Task EnableAccelerationAsync(AmazonS3Client client, string bucketName) { try { var putRequest = new PutBucketAccelerateConfigurationRequest { BucketName = bucketName, AccelerateConfiguration = new AccelerateConfiguration { Status = BucketAccelerateStatus.Enabled, }, }; await client.PutBucketAccelerateConfigurationAsync(putRequest); var getRequest = new GetBucketAccelerateConfigurationRequest { BucketName = bucketName, }; var response = await client.GetBucketAccelerateConfigurationAsync(getRequest); Console.WriteLine($"Acceleration state = '{response.Status}' "); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error occurred. Message:'{ex.Message}' when setting transfer acceleration"); } } }

The following code example shows how to get cross-origin resource sharing (CORS) rules for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Retrieve the CORS configuration applied to the Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used /// to retrieve the CORS configuration.</param> /// <returns>The created CORS configuration object.</returns> private static async Task<CORSConfiguration> RetrieveCORSConfigurationAsync(AmazonS3Client client) { GetCORSConfigurationRequest request = new() { BucketName = BucketName, }; var response = await client.GetCORSConfigurationAsync(request); var configuration = response.Configuration; PrintCORSRules(configuration); return configuration; }
  • For API details, see GetBucketCors in Amazon SDK for .NET API Reference.

The following code example shows how to read data from an object in an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Shows how to download an object from an Amazon S3 bucket to the /// local computer. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <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 static async Task<bool> DownloadObjectFromBucketAsync( IAmazonS3 client, string bucketName, string objectName, string filePath) { // Create a GetObject request var request = new GetObjectRequest { BucketName = bucketName, Key = objectName, }; // Issue request and remember to dispose of the response using GetObjectResponse response = await client.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; } }
  • For API details, see GetObject in Amazon SDK for .NET API Reference.

The following code example shows how to get the access control list (ACL) of an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Get the access control list (ACL) for the new bucket. /// </summary> /// <param name="client">The initialized client object used to get the /// access control list (ACL) of the bucket.</param> /// <param name="newBucketName">The name of the newly created bucket.</param> /// <returns>An S3AccessControlList.</returns> public static async Task<S3AccessControlList> GetACLForBucketAsync(IAmazonS3 client, string newBucketName) { // Retrieve bucket ACL to show that the ACL was properly applied to // the new bucket. GetACLResponse getACLResponse = await client.GetACLAsync(new GetACLRequest { BucketName = newBucketName, }); return getACLResponse.AccessControlList; }
  • For API details, see GetBucketAcl in Amazon SDK for .NET API Reference.

The following code example shows how to get the lifecycle configuration of an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Returns a configuration object for the supplied bucket name. /// </summary> /// <param name="client">The S3 client object used to call /// the GetLifecycleConfigurationAsync method.</param> /// <param name="bucketName">The name of the S3 bucket for which a /// configuration will be created.</param> /// <returns>Returns a new LifecycleConfiguration object.</returns> public static async Task<LifecycleConfiguration> RetrieveLifecycleConfigAsync(IAmazonS3 client, string bucketName) { var request = new GetLifecycleConfigurationRequest() { BucketName = bucketName, }; var response = await client.GetLifecycleConfigurationAsync(request); var configuration = response.Configuration; return configuration; }

The following code example shows how to get the website configuration for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// Get the website configuration. GetBucketWebsiteRequest getRequest = new GetBucketWebsiteRequest() { BucketName = bucketName, }; GetBucketWebsiteResponse getResponse = await client.GetBucketWebsiteAsync(getRequest); Console.WriteLine($"Index document: {getResponse.WebsiteConfiguration.IndexDocumentSuffix}"); Console.WriteLine($"Error document: {getResponse.WebsiteConfiguration.ErrorDocument}");

The following code example shows how to list S3 buckets.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// This example uses the AWS SDK for .NET to list the Amazon Simple Storage /// Service (Amazon S3) buckets belonging to the default account. This code /// was written using AWS SDK for .NET v3.5 and .NET Core 5.0. /// </summary> namespace ListBucketsExample { using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; class ListBuckets { private static IAmazonS3 _s3Client; static async Task Main() { // The client uses the AWS Region of the default user. // If the Region where the buckets were created is different, // pass the Region to the client constructor. For example: // _s3Client = new AmazonS3Client(RegionEndpoint.USEast1); _s3Client = new AmazonS3Client(); var response = await GetBuckets(_s3Client); DisplayBucketList(response.Buckets); } /// <summary> /// Get a list of the buckets owned by the default user. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <returns>The response from the ListingBuckets call that contains a /// list of the buckets owned by the default user.</returns> public static async Task<ListBucketsResponse> GetBuckets(IAmazonS3 client) { return await client.ListBucketsAsync(); } /// <summary> /// This method lists the name and creation date for the buckets in /// the passed List of S3 buckets. /// </summary> /// <param name="bucketList">A List of S3 bucket objects.</param> public static void DisplayBucketList(List<S3Bucket> bucketList) { bucketList .ForEach(b => Console.WriteLine($"Bucket name: {b.BucketName}, created on: {b.CreationDate}")); } } }
  • For API details, see ListBuckets in Amazon SDK for .NET API Reference.

The following code example shows how to list object versions in an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class ListObjectVersions { public static async Task Main() { string bucketName = "doc-example-bucket"; // If the AWS Region where your bucket is defined is different from // the AWS Region where the Amazon S3 bucket is defined, pass the constant // for the AWS Region to the client constructor like this: // var client = new AmazonS3Client(RegionEndpoint.USWest2); IAmazonS3 client = new AmazonS3Client(); await GetObjectListWithAllVersionsAsync(client, bucketName); } /// <summary> /// This method lists all versions of the objects within an Amazon S3 /// version enabled bucket. /// </summary> /// <param name="client">The initialized client object used to call /// ListVersionsAsync.</param> /// <param name="bucketName">The name of the version enabled Amazon S3 bucket /// for which you want to list the versions of the contained objects.</param> public static async Task GetObjectListWithAllVersionsAsync(IAmazonS3 client, string bucketName) { try { // When you instantiate the ListVersionRequest, you can // optionally specify a key name prefix in the request // if you want a list of object versions of a specific object. // For this example we set a small limit in MaxKeys to return // a small list of versions. ListVersionsRequest request = new() { BucketName = bucketName, MaxKeys = 2, }; do { ListVersionsResponse response = await client.ListVersionsAsync(request); // Process response. foreach (S3ObjectVersion entry in response.Versions) { Console.WriteLine($"key: {entry.Key} size: {entry.Size}"); } // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.KeyMarker = response.NextKeyMarker; request.VersionIdMarker = response.NextVersionIdMarker; } else { request = null; } } while (request != null); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: '{ex.Message}'"); } } }

The following code example shows how to list objects in an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Shows how to list the objects in an Amazon S3 bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <param name="bucketName">The name of the bucket for which to list /// the contents.</param> /// <returns>A boolean value indicating the success or failure of the /// copy operation.</returns> public static async Task<bool> ListBucketContentsAsync(IAmazonS3 client, string bucketName) { try { var request = new ListObjectsV2Request { BucketName = bucketName, MaxKeys = 5, }; Console.WriteLine("--------------------------------------"); Console.WriteLine($"Listing the contents of {bucketName}:"); Console.WriteLine("--------------------------------------"); ListObjectsV2Response response; do { response = await client.ListObjectsV2Async(request); response.S3Objects .ForEach(obj => Console.WriteLine($"{obj.Key,-35}{obj.LastModified.ToShortDateString(),10}{obj.Size,10}")); // If the response is truncated, set the request ContinuationToken // from the NextContinuationToken property of the response. request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); return true; } catch (AmazonS3Exception ex) { Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects."); return false; } }

List objects with a paginator.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// The following example lists objects in an Amazon Simple Storage /// Service (Amazon S3) bucket. It was created using AWS SDK for .NET 3.5 /// and .NET Core 5.0. /// </summary> public class ListObjectsPaginator { private const string BucketName = "doc-example-bucket"; public static async Task Main() { IAmazonS3 s3Client = new AmazonS3Client(); Console.WriteLine($"Listing the objects contained in {BucketName}:\n"); await ListingObjectsAsync(s3Client, BucketName); } /// <summary> /// This method uses a paginator to retrieve the list of objects in an /// an Amazon S3 bucket. /// </summary> /// <param name="client">An Amazon S3 client object.</param> /// <param name="bucketName">The name of the S3 bucket whose objects /// you want to list.</param> public static async Task ListingObjectsAsync(IAmazonS3 client, string bucketName) { var listObjectsV2Paginator = client.Paginators.ListObjectsV2(new ListObjectsV2Request { BucketName = bucketName, }); await foreach (var response in listObjectsV2Paginator.Responses) { Console.WriteLine($"HttpStatusCode: {response.HttpStatusCode}"); Console.WriteLine($"Number of Keys: {response.KeyCount}"); foreach (var entry in response.S3Objects) { Console.WriteLine($"Key = {entry.Key} Size = {entry.Size}"); } } } }
  • For API details, see ListObjects in Amazon SDK for .NET API Reference.

The following code example shows how to restore an archived copy of an object back into an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class RestoreArchivedObject { public static void Main() { string bucketName = "doc-example-bucket"; string objectKey = "archived-object.txt"; // Specify your bucket region (an example region is shown). RegionEndpoint bucketRegion = RegionEndpoint.USWest2; IAmazonS3 client = new AmazonS3Client(bucketRegion); RestoreObjectAsync(client, bucketName, objectKey).Wait(); } /// <summary> /// This method restores an archived object from an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// RestoreObjectAsync.</param> /// <param name="bucketName">A string representing the name of the /// bucket where the object was located before it was archived.</param> /// <param name="objectKey">A string representing the name of the /// archived object to restore.</param> public static async Task RestoreObjectAsync(IAmazonS3 client, string bucketName, string objectKey) { try { var restoreRequest = new RestoreObjectRequest { BucketName = bucketName, Key = objectKey, Days = 2, }; RestoreObjectResponse response = await client.RestoreObjectAsync(restoreRequest); // Check the status of the restoration. await CheckRestorationStatusAsync(client, bucketName, objectKey); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine($"Error: {amazonS3Exception.Message}"); } } /// <summary> /// This method retrieves the status of the object's restoration. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// GetObjectMetadataAsync.</param> /// <param name="bucketName">A string representing the name of the Amazon /// S3 bucket which contains the archived object.</param> /// <param name="objectKey">A string representing the name of the /// archived object you want to restore.</param> public static async Task CheckRestorationStatusAsync(IAmazonS3 client, string bucketName, string objectKey) { GetObjectMetadataRequest metadataRequest = new() { BucketName = bucketName, Key = objectKey, }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); var restStatus = response.RestoreInProgress ? "in-progress" : "finished or failed"; Console.WriteLine($"Restoration status: {restStatus}"); } }
  • For API details, see RestoreObject in Amazon SDK for .NET API Reference.

The following code example shows how to set a new access control list (ACL) for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Creates an Amazon S3 bucket with an ACL to control access to the /// bucket and the objects stored in it. /// </summary> /// <param name="client">The initialized client object used to create /// an Amazon S3 bucket, with an ACL applied to the bucket. /// </param> /// <param name="region">The AWS Region where the bucket will be created.</param> /// <param name="newBucketName">The name of the bucket to create.</param> /// <returns>A boolean value indicating success or failure.</returns> public static async Task<bool> CreateBucketUseCannedACLAsync(IAmazonS3 client, S3Region region, string newBucketName) { try { // Create a new Amazon S3 bucket with Canned ACL. var putBucketRequest = new PutBucketRequest() { BucketName = newBucketName, BucketRegion = region, CannedACL = S3CannedACL.LogDeliveryWrite, }; PutBucketResponse putBucketResponse = await client.PutBucketAsync(putBucketRequest); return putBucketResponse.HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine($"Amazon S3 error: {ex.Message}"); } return false; }
  • For API details, see PutBucketAcl in Amazon SDK for .NET API Reference.

The following code example shows how to set the website configuration for an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

// Put the website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument, }, }; PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest);

The following code example shows how to upload an object to an S3 bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Shows how to upload a file from the local computer to an Amazon S3 /// bucket. /// </summary> /// <param name="client">An initialized Amazon S3 client object.</param> /// <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 static async Task<bool> UploadFileAsync( IAmazonS3 client, string bucketName, string objectName, string filePath) { var request = new PutObjectRequest { BucketName = bucketName, Key = objectName, FilePath = filePath, }; var response = await client.PutObjectAsync(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully uploaded {objectName} to {bucketName}."); return true; } else { Console.WriteLine($"Could not upload {objectName} to {bucketName}."); return false; } }

Upload an object with server-side encryption.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class ServerSideEncryption { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "samplefile.txt"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USWest2. IAmazonS3 client = new AmazonS3Client(); await WritingAnObjectAsync(client, bucketName, keyName); } /// <summary> /// Upload a sample object include a setting for encryption. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to /// to upload a file and apply server-side encryption.</param> /// <param name="bucketName">The name of the Amazon S3 bucket where the /// encrypted object will reside.</param> /// <param name="keyName">The name for the object that you want to /// create in the supplied bucket.</param> public static async Task WritingAnObjectAsync(IAmazonS3 client, string bucketName, string keyName) { try { var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, ContentBody = "sample text", ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256, }; var putResponse = await client.PutObjectAsync(putRequest); // Determine the encryption state of an object. GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = keyName, }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); ServerSideEncryptionMethod objectEncryption = response.ServerSideEncryptionMethod; Console.WriteLine($"Encryption method used: {0}", objectEncryption.ToString()); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: '{ex.Message}' when writing an object"); } } }
  • For API details, see PutObject in Amazon SDK for .NET API Reference.

Scenarios

The following code example shows how to create a presigned URL for S3 and upload an object.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Generate a presigned URL that can perform an Amazon S3 action for a limited time.

using System; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class GenPresignedUrl { public static void Main() { const string bucketName = "doc-example-bucket"; const string objectKey = "sample.txt"; // Specify how long the presigned URL lasts, in hours const double timeoutDuration = 12; // Specify the AWS Region of your Amazon S3 bucket if it is // different from the Region defined for the default user, // pass the Region to the constructor for the client. For // example: // RegionEndpoint bucketRegion = RegionEndpoint.USWest2; IAmazonS3 s3Client = new AmazonS3Client(); string urlString = GeneratePresignedURL(s3Client, bucketName, objectKey, timeoutDuration); Console.WriteLine($"The generated URL is: {urlString}."); } /// <summary> /// Generate a presigned URL that can be used to access the file named /// in the objectKey parameter for the amount of time specified in the /// duration parameter. /// </summary> /// <param name="client">An initialized S3 client object used to call /// the GetPresignedUrl method.</param> /// <param name="bucketName">The name of the S3 bucket containing the /// object for which to create the presigned URL.</param> /// <param name="objectKey">The name of the object to access with the /// presigned URL.</param> /// <param name="duration">The length of time for which the presigned /// URL will be valid.</param> /// <returns>A string representing the generated presigned URL.</returns> public static string GeneratePresignedURL(IAmazonS3 client, string bucketName, string objectKey, double duration) { string urlString = string.Empty; try { var request = new GetPreSignedUrlRequest() { BucketName = bucketName, Key = objectKey, Expires = DateTime.UtcNow.AddHours(duration), }; urlString = client.GetPreSignedURL(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error:'{ex.Message}'"); } return urlString; } }

Generate a presigned URL and perform an upload using that URL.

using System; using System.IO; using System.Net; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class UploadUsingPresignedURL { public static void Main() { string bucketName = "doc-example-bucket"; string keyName = "samplefile.txt"; string filePath = $"source\\{keyName}"; // Specify how long the signed URL will be valid in hours. double timeoutDuration = 12; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USWest2. IAmazonS3 client = new AmazonS3Client(); var url = GeneratePreSignedURL(client, bucketName, keyName, timeoutDuration); var success = UploadObject(filePath, url); if (success) { Console.WriteLine("Upload succeeded."); } else { Console.WriteLine("Upload failed."); } } /// <summary> /// Uploads an object to an Amazon S3 bucket using the presigned URL passed in /// the url parameter. /// </summary> /// <param name="filePath">The path (including file name) to the local /// file you want to upload.</param> /// <param name="url">The presigned URL that will be used to upload the /// file to the Amazon S3 bucket.</param> /// <returns>A Boolean value indicating the success or failure of the /// operation, based on the HttpWebResponse.</returns> public static bool UploadObject(string filePath, string url) { HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest; httpRequest.Method = "PUT"; using (Stream dataStream = httpRequest.GetRequestStream()) { var buffer = new byte[8000]; using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) { dataStream.Write(buffer, 0, bytesRead); } } } HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse; return response.StatusCode == HttpStatusCode.OK; } /// <summary> /// Generates a presigned URL which will be used to upload an object to /// an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// GetPreSignedURL.</param> /// <param name="bucketName">The name of the Amazon S3 bucket to which the /// presigned URL will point.</param> /// <param name="objectKey">The name of the file that will be uploaded.</param> /// <param name="duration">How long (in hours) the presigned URL will /// be valid.</param> /// <returns>The generated URL.</returns> public static string GeneratePreSignedURL( IAmazonS3 client, string bucketName, string objectKey, double duration) { var request = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Verb = HttpVerb.PUT, Expires = DateTime.UtcNow.AddHours(duration), }; string url = client.GetPreSignedURL(request); return url; } }

The following code example shows how to:

  • Create a bucket.

  • Upload a file to the bucket.

  • Download an object from a bucket.

  • Copy an object to a subfolder in a bucket.

  • List the objects in a bucket.

  • Delete the objects in a bucket.

  • Delete a bucket.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

public class S3_Basics { public static async Task Main() { // Create an Amazon S3 client object. The constructor uses the // default user installed on the system. To work with Amazon S3 // features in a different AWS Region, pass the AWS Region as a // parameter to the client constructor. IAmazonS3 client = new AmazonS3Client(); string bucketName = string.Empty; string filePath = string.Empty; string keyName = string.Empty; var sepBar = new string('-', Console.WindowWidth); 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); // Create a bucket. Console.WriteLine($"\n{sepBar}"); Console.WriteLine("\nCreate a new Amazon S3 bucket.\n"); Console.WriteLine(sepBar); Console.Write("Please enter a name for the new bucket: "); bucketName = Console.ReadLine(); var success = await S3Bucket.CreateBucketAsync(client, 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); // 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; } } // Get the file name from the full path. keyName = Path.GetFileName(filePath); success = await S3Bucket.UploadFileAsync(client, bucketName, keyName, filePath); if (success) { Console.WriteLine($"Successfully uploaded {keyName} from {filePath} to {bucketName}.\n"); } else { Console.WriteLine($"Could not upload {keyName}.\n"); } // Set the file path to an empty string to avoid overwriting the // file we just uploaded to the bucket. filePath = string.Empty; // Now get a new location where we can save the file. while (string.IsNullOrEmpty(filePath)) { // First get the path to which the file will be downloaded. Console.Write("Please enter the path where the file will be downloaded: "); filePath = Console.ReadLine(); // Confirm that the file exists on the local computer. if (File.Exists($"{filePath}\\{keyName}")) { Console.WriteLine($"Sorry, the file already exists in that location.\n"); filePath = string.Empty; } } // Download an object from a bucket. success = await S3Bucket.DownloadObjectFromBucketAsync(client, bucketName, keyName, filePath); 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; while (string.IsNullOrEmpty(folderName)) { Console.Write("Please enter the name of the folder to copy your object to: "); folderName = Console.ReadLine(); } while (string.IsNullOrEmpty(keyName)) { // Get the name to give to the object once uploaded. Console.Write("Enter the name of the object to copy: "); keyName = Console.ReadLine(); } await S3Bucket.CopyObjectInBucketAsync(client, bucketName, keyName, folderName); // List the objects in the bucket. await S3Bucket.ListBucketContentsAsync(client, bucketName); // Delete the contents of the bucket. await S3Bucket.DeleteBucketContentsAsync(client, bucketName); // Deleting the bucket too quickly after deleting its contents will // cause an error that the bucket isn't empty. So... Console.WriteLine("Press <Enter> when you are ready to delete the bucket."); _ = Console.ReadLine(); // Delete the bucket. await S3Bucket.DeleteBucketAsync(client, bucketName); } }

The following code example shows how to get started with encryption for Amazon S3 objects.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.IO; using System.Security.Cryptography; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class SSEClientEncryption { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "exampleobject.txt"; string copyTargetKeyName = "examplecopy.txt"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USWest2. IAmazonS3 client = new AmazonS3Client(); try { // Create an encryption key. Aes aesEncryption = Aes.Create(); aesEncryption.KeySize = 256; aesEncryption.GenerateKey(); string base64Key = Convert.ToBase64String(aesEncryption.Key); // Upload the object. PutObjectRequest putObjectRequest = await UploadObjectAsync(client, bucketName, keyName, base64Key); // Download the object and verify that its contents match what you uploaded. await DownloadObjectAsync(client, bucketName, keyName, base64Key, putObjectRequest); // Get object metadata and verify that the object uses AES-256 encryption. await GetObjectMetadataAsync(client, bucketName, keyName, base64Key); // Copy both the source and target objects using server-side encryption with // an encryption key. await CopyObjectAsync(client, bucketName, keyName, copyTargetKeyName, aesEncryption, base64Key); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } /// <summary> /// Uploads an object to an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// PutObjectAsync.</param> /// <param name="bucketName">The name of the Amazon S3 bucket to which the /// object will be uploaded.</param> /// <param name="keyName">The name of the object to upload to the Amazon S3 /// bucket.</param> /// <param name="base64Key">The encryption key.</param> /// <returns>The PutObjectRequest object for use by DownloadObjectAsync.</returns> public static async Task<PutObjectRequest> UploadObjectAsync( IAmazonS3 client, string bucketName, string keyName, string base64Key) { PutObjectRequest putObjectRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, ContentBody = "sample text", ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key, }; PutObjectResponse putObjectResponse = await client.PutObjectAsync(putObjectRequest); return putObjectRequest; } /// <summary> /// Downloads an encrypted object from an Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// GetObjectAsync.</param> /// <param name="bucketName">The name of the Amazon S3 bucket where the object /// is located.</param> /// <param name="keyName">The name of the Amazon S3 object to download.</param> /// <param name="base64Key">The encryption key used to encrypt the /// object.</param> /// <param name="putObjectRequest">The PutObjectRequest used to upload /// the object.</param> public static async Task DownloadObjectAsync( IAmazonS3 client, string bucketName, string keyName, string base64Key, PutObjectRequest putObjectRequest) { GetObjectRequest getObjectRequest = new GetObjectRequest { BucketName = bucketName, Key = keyName, // Provide encryption information for the object stored in Amazon S3. ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key, }; using (GetObjectResponse getResponse = await client.GetObjectAsync(getObjectRequest)) using (StreamReader reader = new StreamReader(getResponse.ResponseStream)) { string content = reader.ReadToEnd(); if (string.Compare(putObjectRequest.ContentBody, content) == 0) { Console.WriteLine("Object content is same as we uploaded"); } else { Console.WriteLine("Error...Object content is not same."); } if (getResponse.ServerSideEncryptionCustomerMethod == ServerSideEncryptionCustomerMethod.AES256) { Console.WriteLine("Object encryption method is AES256, same as we set"); } else { Console.WriteLine("Error...Object encryption method is not the same as AES256 we set"); } } } /// <summary> /// Retrieves the metadata associated with an Amazon S3 object. /// </summary> /// <param name="client">The initialized Amazon S3 client object used /// to call GetObjectMetadataAsync.</param> /// <param name="bucketName">The name of the Amazon S3 bucket containing the /// object for which we want to retrieve metadata.</param> /// <param name="keyName">The name of the object for which we wish to /// retrieve the metadata.</param> /// <param name="base64Key">The encryption key associated with the /// object.</param> public static async Task GetObjectMetadataAsync( IAmazonS3 client, string bucketName, string keyName, string base64Key) { GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = keyName, // The object stored in Amazon S3 is encrypted, so provide the necessary encryption information. ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key, }; GetObjectMetadataResponse getObjectMetadataResponse = await client.GetObjectMetadataAsync(getObjectMetadataRequest); Console.WriteLine("The object metadata show encryption method used is: {0}", getObjectMetadataResponse.ServerSideEncryptionCustomerMethod); } /// <summary> /// Copies an encrypted object from one Amazon S3 bucket to another. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// CopyObjectAsync.</param> /// <param name="bucketName">The Amazon S3 bucket containing the object /// to copy.</param> /// <param name="keyName">The name of the object to copy.</param> /// <param name="copyTargetKeyName">The Amazon S3 bucket to which the object /// will be copied.</param> /// <param name="aesEncryption">The encryption type to use.</param> /// <param name="base64Key">The encryption key to use.</param> public static async Task CopyObjectAsync( IAmazonS3 client, string bucketName, string keyName, string copyTargetKeyName, Aes aesEncryption, string base64Key) { aesEncryption.GenerateKey(); string copyBase64Key = Convert.ToBase64String(aesEncryption.Key); CopyObjectRequest copyRequest = new CopyObjectRequest { SourceBucket = bucketName, SourceKey = keyName, DestinationBucket = bucketName, DestinationKey = copyTargetKeyName, // Information about the source object's encryption. CopySourceServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, CopySourceServerSideEncryptionCustomerProvidedKey = base64Key, // Information about the target object's encryption. ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = copyBase64Key, }; await client.CopyObjectAsync(copyRequest); } }

The following code example shows how to get started with tags for Amazon S3 objects.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class ObjectTag { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "newobject.txt"; string filePath = @"*** file path ***"; // Specify your bucket region (an example region is shown). RegionEndpoint bucketRegion = RegionEndpoint.USWest2; var client = new AmazonS3Client(bucketRegion); await PutObjectsWithTagsAsync(client, bucketName, keyName, filePath); } /// <summary> /// This method uploads an object with tags. It then shows the tag /// values, changes the tags, and shows the new tags. /// </summary> /// <param name="client">The Initialized Amazon S3 client object used /// to call the methods to create and change an objects tags.</param> /// <param name="bucketName">A string representing the name of the /// bucket where the object will be stored.</param> /// <param name="keyName">A string representing the key name of the /// object to be tagged.</param> /// <param name="filePath">The directory location and file name of the /// object to be uploaded to the Amazon S3 bucket.</param> public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath) { try { // Create an object with tags. var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, TagSet = new List<Tag> { new Tag { Key = "Keyx1", Value = "Value1" }, new Tag { Key = "Keyx2", Value = "Value2" }, }, }; PutObjectResponse response = await client.PutObjectAsync(putRequest); // Now retrieve the new object's tags. GetObjectTaggingRequest getTagsRequest = new() { BucketName = bucketName, Key = keyName, }; GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); // Display the tag values. objectTags.Tagging .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); Tagging newTagSet = new() { TagSet = new List<Tag> { new Tag { Key = "Key3", Value = "Value3" }, new Tag { Key = "Key4", Value = "Value4" }, }, }; PutObjectTaggingRequest putObjTagsRequest = new () { BucketName = bucketName, Key = keyName, Tagging = newTagSet, }; PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest); // Retrieve the tags again and show the values. GetObjectTaggingRequest getTagsRequest2 = new() { BucketName = bucketName, Key = keyName, }; GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2); objectTags2.Tagging .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); } catch (AmazonS3Exception ex) { Console.WriteLine( $"Error: '{ex.Message}'"); } } }

The following code example shows how to manage access control lists (ACLs) for Amazon S3 buckets.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; public class ManageACLs { public static async Task Main() { string bucketName = "doc-example-bucket1"; string newBucketName = "doc-example-bucket2"; string keyName = "sample-object.txt"; string emailAddress = "someone@example.com"; // If the AWS Region where your bucket is located is different from // the Region defined for the default user, pass the Amazon S3 bucket's // name to the client constructor. It should look like this: // RegionEndpoint bucketRegion = RegionEndpoint.USEast1; IAmazonS3 client = new AmazonS3Client(); await TestBucketObjectACLsAsync(client, bucketName, newBucketName, keyName, emailAddress); } /// <summary> /// Creates a new Amazon S3 bucket with a canned ACL, then retrieves the ACL /// information and then adds a new ACL to one of the objects in the /// Amazon S3 bucket. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to call /// methods to create a bucket, get an ACL, and add a different ACL to /// one of the objects.</param> /// <param name="bucketName">A string representing the original Amazon S3 /// bucket name.</param> /// <param name="newBucketName">A string representing the name of the /// new bucket that will be created.</param> /// <param name="keyName">A string representing the key name of an Amazon S3 /// object for which we will change the ACL.</param> /// <param name="emailAddress">A string representing the email address /// belonging to the person to whom access to the Amazon S3 bucket will be /// granted.</param> public static async Task TestBucketObjectACLsAsync( IAmazonS3 client, string bucketName, string newBucketName, string keyName, string emailAddress) { try { // Create a new Amazon S3 bucket and specify canned ACL. var success = await CreateBucketWithCannedACLAsync(client, newBucketName); // Get the ACL on a bucket. await GetBucketACLAsync(client, bucketName); // Add (replace) the ACL on an object in a bucket. await AddACLToExistingObjectAsync(client, bucketName, keyName, emailAddress); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine($"Exception: {amazonS3Exception.Message}"); } } /// <summary> /// Creates a new Amazon S3 bucket with a canned ACL attached. /// </summary> /// <param name="client">The initialized client object used to call /// PutBucketAsync.</param> /// <param name="newBucketName">A string representing the name of the /// new Amazon S3 bucket.</param> /// <returns>Returns a boolean value indicating success or failure.</returns> public static async Task<bool> CreateBucketWithCannedACLAsync(IAmazonS3 client, string newBucketName) { var request = new PutBucketRequest() { BucketName = newBucketName, BucketRegion = S3Region.EUW1, // Add a canned ACL. CannedACL = S3CannedACL.LogDeliveryWrite, }; var response = await client.PutBucketAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; } /// <summary> /// Retrieves the ACL associated with the Amazon S3 bucket name in the /// bucketName parameter. /// </summary> /// <param name="client">The initialized client object used to call /// PutBucketAsync.</param> /// <param name="bucketName">The Amazon S3 bucket for which we want to get the /// ACL list.</param> /// <returns>Returns an S3AccessControlList returned from the call to /// GetACLAsync.</returns> public static async Task<S3AccessControlList> GetBucketACLAsync(IAmazonS3 client, string bucketName) { GetACLResponse response = await client.GetACLAsync(new GetACLRequest { BucketName = bucketName, }); return response.AccessControlList; } /// <summary> /// Adds a new ACL to an existing object in the Amazon S3 bucket. /// </summary> /// <param name="client">The initialized client object used to call /// PutBucketAsync.</param> /// <param name="bucketName">A string representing the name of the Amazon S3 /// bucket containing the object to which we want to apply a new ACL.</param> /// <param name="keyName">A string representing the name of the object /// to which we want to apply the new ACL.</param> /// <param name="emailAddress">The email address of the person to whom /// we will be applying to whom access will be granted.</param> public static async Task AddACLToExistingObjectAsync(IAmazonS3 client, string bucketName, string keyName, string emailAddress) { // Retrieve the ACL for an object. GetACLResponse aclResponse = await client.GetACLAsync(new GetACLRequest { BucketName = bucketName, Key = keyName, }); S3AccessControlList acl = aclResponse.AccessControlList; // Retrieve the owner. Owner owner = acl.Owner; // Clear existing grants. acl.Grants.Clear(); // Add a grant to reset the owner's full permission // (the previous clear statement removed all permissions). var fullControlGrant = new S3Grant { Grantee = new S3Grantee { CanonicalUser = acl.Owner.Id }, }; acl.AddGrant(fullControlGrant.Grantee, S3Permission.FULL_CONTROL); // Specify email to identify grantee for granting permissions. var grantUsingEmail = new S3Grant { Grantee = new S3Grantee { EmailAddress = emailAddress }, Permission = S3Permission.WRITE_ACP, }; // Specify log delivery group as grantee. var grantLogDeliveryGroup = new S3Grant { Grantee = new S3Grantee { URI = "http://acs.amazonaws.com/groups/s3/LogDelivery" }, Permission = S3Permission.WRITE, }; // Create a new ACL. var newAcl = new S3AccessControlList { Grants = new List<S3Grant> { grantUsingEmail, grantLogDeliveryGroup }, Owner = owner, }; // Set the new ACL. We're throwing away the response here. _ = await client.PutACLAsync(new PutACLRequest { BucketName = bucketName, Key = keyName, AccessControlList = newAcl, }); } }

The following code example shows how to perform a multipart copy of an Amazon S3 object.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class MPUapiCopyObj { private const string SourceBucket = "doc-example-bucket1"; private const string TargetBucket = "doc-example-bucket2"; private const string SourceObjectKey = "example.mov"; private const string TargetObjectKey = "copied_video_file.mov"; /// <summary> /// This method starts the multi-part upload. /// </summary> public static async Task Main() { var s3Client = new AmazonS3Client(); Console.WriteLine("Copying object..."); await MPUCopyObjectAsync(s3Client); } /// <summary> /// This method uses the passed client object to perform a multipart /// copy operation. /// </summary> /// <param name="client">An Amazon S3 client object that will be used /// to perform the copy.</param> public static async Task MPUCopyObjectAsync(AmazonS3Client client) { // Create a list to store the copy part responses. var copyResponses = new List<CopyPartResponse>(); // Setup information required to initiate the multipart upload. var initiateRequest = new InitiateMultipartUploadRequest { BucketName = TargetBucket, Key = TargetObjectKey, }; // Initiate the upload. InitiateMultipartUploadResponse initResponse = await client.InitiateMultipartUploadAsync(initiateRequest); // Save the upload ID. string uploadId = initResponse.UploadId; try { // Get the size of the object. var metadataRequest = new GetObjectMetadataRequest { BucketName = SourceBucket, Key = SourceObjectKey, }; GetObjectMetadataResponse metadataResponse = await client.GetObjectMetadataAsync(metadataRequest); var objectSize = metadataResponse.ContentLength; // Length in bytes. // Copy the parts. var partSize = 5 * (long)Math.Pow(2, 20); // Part size is 5 MB. long bytePosition = 0; for (int i = 1; bytePosition < objectSize; i++) { var copyRequest = new CopyPartRequest { DestinationBucket = TargetBucket, DestinationKey = TargetObjectKey, SourceBucket = SourceBucket, SourceKey = SourceObjectKey, UploadId = uploadId, FirstByte = bytePosition, LastByte = bytePosition + partSize - 1 >= objectSize ? objectSize - 1 : bytePosition + partSize - 1, PartNumber = i, }; copyResponses.Add(await client.CopyPartAsync(copyRequest)); bytePosition += partSize; } // Set up to complete the copy. var completeRequest = new CompleteMultipartUploadRequest { BucketName = TargetBucket, Key = TargetObjectKey, UploadId = initResponse.UploadId, }; completeRequest.AddPartETags(copyResponses); // Complete the copy. CompleteMultipartUploadResponse completeUploadResponse = await client.CompleteMultipartUploadAsync(completeRequest); } catch (AmazonS3Exception e) { Console.WriteLine($"Error encountered on server. Message:'{e.Message}' when writing an object"); } catch (Exception e) { Console.WriteLine($"Unknown encountered on server. Message:'{e.Message}' when writing an object"); } } }

The following code example shows how to upload or download large files to and from Amazon S3.

For more information, see Uploading an object using multipart upload.

Amazon SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Call functions that transfer files to and from an S3 bucket using the Amazon S3 TransferUtility.

global using System.Text; global using Amazon; global using Amazon.S3; global using Amazon.S3.Model; global using Amazon.S3.Transfer; global using TransferUtilityBasics; // This Amazon S3 client uses the default user credentials // defined for this computer. using Microsoft.Extensions.Configuration; IAmazonS3 client = new AmazonS3Client(); var transferUtil = new TransferUtility(client); IConfiguration _configuration; _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from JSON file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); // Edit the values in settings.json to use an S3 bucket and files that // exist on your AWS account and on the local computer where you // run this scenario. var bucketName = _configuration["BucketName"]; var localPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\TransferFolder"; DisplayInstructions(); PressEnter(); Console.WriteLine(); // Upload a single file to an S3 bucket. DisplayTitle("Upload a single file"); var fileToUpload = _configuration["FileToUpload"]; Console.WriteLine($"Uploading {fileToUpload} to the S3 bucket, {bucketName}."); var success = await TransferMethods.UploadSingleFileAsync(transferUtil, bucketName, fileToUpload, localPath); if (success) { Console.WriteLine($"Successfully uploaded the file, {fileToUpload} to {bucketName}."); } PressEnter(); // Upload a local directory to an S3 bucket. DisplayTitle("Upload all files from a local directory"); Console.WriteLine("Upload all the files in a local folder to an S3 bucket."); const string keyPrefix = "UploadFolder"; var uploadPath = $"{localPath}\\UploadFolder"; Console.WriteLine($"Uploading the files in {uploadPath} to {bucketName}"); DisplayTitle($"{uploadPath} files"); DisplayLocalFiles(uploadPath); Console.WriteLine(); PressEnter(); success = await TransferMethods.UploadFullDirectoryAsync(transferUtil, bucketName, keyPrefix, uploadPath); if (success) { Console.WriteLine($"Successfully uploaded the files in {uploadPath} to {bucketName}."); Console.WriteLine($"{bucketName} currently contains the following files:"); await DisplayBucketFiles(client, bucketName, keyPrefix); Console.WriteLine(); } PressEnter(); // Download a single file from an S3 bucket. DisplayTitle("Download a single file"); Console.WriteLine("Now we will download a single file from an S3 bucket."); var keyName = _configuration["FileToDownload"]; Console.WriteLine($"Downloading {keyName} from {bucketName}."); success = await TransferMethods.DownloadSingleFileAsync(transferUtil, bucketName, keyName, localPath); if (success) { Console.WriteLine("$Successfully downloaded the file, {keyName} from {bucketName}."); } PressEnter(); // Download the contents of a directory from an S3 bucket. DisplayTitle("Download the contents of an S3 bucket"); var s3Path = _configuration["S3Path"]; var downloadPath = $"{localPath}\\{s3Path}"; Console.WriteLine($"Downloading the contents of {bucketName}\\{s3Path}"); Console.WriteLine($"{bucketName}\\{s3Path} contains the following files:"); await DisplayBucketFiles(client, bucketName, s3Path); Console.WriteLine(); success = await TransferMethods.DownloadS3DirectoryAsync(transferUtil, bucketName, s3Path, downloadPath); if (success) { Console.WriteLine($"Downloaded the files in {bucketName} to {downloadPath}."); Console.WriteLine($"{downloadPath} now contains the following files:"); DisplayLocalFiles(downloadPath); } Console.WriteLine("\nThe TransferUtility Basics application has completed."); PressEnter(); // Displays the title for a section of the scenario. static void DisplayTitle(string titleText) { var sepBar = new string('-', Console.WindowWidth); Console.WriteLine(sepBar); Console.WriteLine(CenterText(titleText)); Console.WriteLine(sepBar); } // Displays a description of the actions to be performed by the scenario. static void DisplayInstructions() { var sepBar = new string('-', Console.WindowWidth); DisplayTitle("Amazon S3 Transfer Utility Basics"); Console.WriteLine("This program shows how to use the Amazon S3 Transfer Utility."); Console.WriteLine("It performs the following actions:"); Console.WriteLine("\t1. Upload a single object to an S3 bucket."); Console.WriteLine("\t2. Upload an entire directory from the local computer to an\n\t S3 bucket."); Console.WriteLine("\t3. Download a single object from an S3 bucket."); Console.WriteLine("\t4. Download the objects in an S3 bucket to a local directory."); Console.WriteLine($"\n{sepBar}"); } // Pauses the scenario. static void PressEnter() { Console.WriteLine("Press <Enter> to continue."); _ = Console.ReadLine(); Console.WriteLine("\n"); } // Returns the string textToCenter, padded on the left with spaces // that center the text on the console display. static string CenterText(string textToCenter) { var centeredText = new StringBuilder(); var screenWidth = Console.WindowWidth; centeredText.Append(new string(' ', (int)(screenWidth - textToCenter.Length) / 2)); centeredText.Append(textToCenter); return centeredText.ToString(); } // Displays a list of file names included in the specified path. static void DisplayLocalFiles(string localPath) { var fileList = Directory.GetFiles(localPath); if (fileList.Length > 0) { foreach (var fileName in fileList) { Console.WriteLine(fileName); } } } // Displays a list of the files in the specified S3 bucket and prefix. static async Task DisplayBucketFiles(IAmazonS3 client, string bucketName, string s3Path) { ListObjectsV2Request request = new() { BucketName = bucketName, Prefix = s3Path, MaxKeys = 5, }; var response = new ListObjectsV2Response(); do { response = await client.ListObjectsV2Async(request); response.S3Objects .ForEach(obj => Console.WriteLine($"{obj.Key}")); // If the response is truncated, set the request ContinuationToken // from the NextContinuationToken property of the response. request.ContinuationToken = response.NextContinuationToken; } while (response.IsTruncated); }

Upload a single file.

/// <summary> /// Uploads a single file from the local computer to an S3 bucket. /// </summary> /// <param name="transferUtil">The transfer initialized TransferUtility /// object.</param> /// <param name="bucketName">The name of the S3 bucket where the file /// will be stored.</param> /// <param name="fileName">The name of the file to upload.</param> /// <param name="localPath">The local path where the file is stored.</param> /// <returns>A boolean value indicating the success of the action.</returns> public static async Task<bool> UploadSingleFileAsync( TransferUtility transferUtil, string bucketName, string fileName, string localPath) { if (File.Exists($"{localPath}\\{fileName}")) { try { await transferUtil.UploadAsync(new TransferUtilityUploadRequest { BucketName = bucketName, Key = fileName, FilePath = $"{localPath}\\{fileName}", }); return true; } catch (AmazonS3Exception s3Ex) { Console.WriteLine($"Could not upload {fileName} from {localPath} because:"); Console.WriteLine(s3Ex.Message); return false; } } else { Console.WriteLine($"{fileName} does not exist in {localPath}"); return false; } }

Upload an entire local directory.

/// <summary> /// Uploads all the files in a local directory to a directory in an S3 /// bucket. /// </summary> /// <param name="transferUtil">The transfer initialized TransferUtility /// object.</param> /// <param name="bucketName">The name of the S3 bucket where the files /// will be stored.</param> /// <param name="keyPrefix">The key prefix is the S3 directory where /// the files will be stored.</param> /// <param name="localPath">The local directory that contains the files /// to be uploaded.</param> /// <returns>A Boolean value representing the success of the action.</returns> public static async Task<bool> UploadFullDirectoryAsync( TransferUtility transferUtil, string bucketName, string keyPrefix, string localPath) { if (Directory.Exists(localPath)) { try { await transferUtil.UploadDirectoryAsync(new TransferUtilityUploadDirectoryRequest { BucketName = bucketName, KeyPrefix = keyPrefix, Directory = localPath, }); return true; } catch (AmazonS3Exception s3Ex) { Console.WriteLine($"Can't upload the contents of {localPath} because:"); Console.WriteLine(s3Ex?.Message); return false; } } else { Console.WriteLine($"The directory {localPath} does not exist."); return false; } }

Download a single file.

/// <summary> /// Download a single file from an S3 bucket to the local computer. /// </summary> /// <param name="transferUtil">The transfer initialized TransferUtility /// object.</param> /// <param name="bucketName">The name of the S3 bucket containing the /// file to download.</param> /// <param name="keyName">The name of the file to download.</param> /// <param name="localPath">The path on the local computer where the /// downloaded file will be saved.</param> /// <returns>A Boolean value indicating the results of the action.</returns> public static async Task<bool> DownloadSingleFileAsync( TransferUtility transferUtil, string bucketName, string keyName, string localPath) { await transferUtil.DownloadAsync(new TransferUtilityDownloadRequest { BucketName = bucketName, Key = keyName, FilePath = $"{localPath}\\{keyName}", }); return (File.Exists($"{localPath}\\{keyName}")); }

Download contents of an S3 bucket.

/// <summary> /// Downloads the contents of a directory in an S3 bucket to a /// directory on the local computer. /// </summary> /// <param name="transferUtil">The transfer initialized TransferUtility /// object.</param> /// <param name="bucketName">The bucket containing the files to download.</param> /// <param name="s3Path">The S3 directory where the files are located.</param> /// <param name="localPath">The local path to which the files will be /// saved.</param> /// <returns>A Boolean value representing the success of the action.</returns> public static async Task<bool> DownloadS3DirectoryAsync( TransferUtility transferUtil, string bucketName, string s3Path, string localPath) { int fileCount = 0; // If the directory doesn't exist, it will be created. if (Directory.Exists(s3Path)) { var files = Directory.GetFiles(localPath); fileCount = files.Length; } await transferUtil.DownloadDirectoryAsync(new TransferUtilityDownloadDirectoryRequest { BucketName = bucketName, LocalDirectory = localPath, S3Directory = s3Path, }); if (Directory.Exists(localPath)) { var files = Directory.GetFiles(localPath); if (files.Length > fileCount) { return true; } // No change in the number of files. Assume // the download failed. return false; } // The local directory doesn't exist. No files // were downloaded. return false; }

Track the progress of an upload using the TransferUtility.

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; public class TrackMPUUsingHighLevelAPI { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "sample_pic.png"; string path = "filepath/directory/"; string filePath = $"{path}{keyName}"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USWest2 or RegionEndpoint.USEast2. IAmazonS3 client = new AmazonS3Client(); await TrackMPUAsync(client, bucketName, filePath, keyName); } /// <summary> /// Starts an Amazon S3 multipart upload and assigns an event handler to /// track the progress of the upload. /// </summary> /// <param name="client">The initialized Amazon S3 client object used to /// perform the multipart upload.</param> /// <param name="bucketName">The name of the bucket to which to upload /// the file.</param> /// <param name="filePath">The path, including the file name of the /// file to be uploaded to the Amazon S3 bucket.</param> /// <param name="keyName">The file name to be used in the /// destination Amazon S3 bucket.</param> public static async Task TrackMPUAsync( IAmazonS3 client, string bucketName, string filePath, string keyName) { try { var fileTransferUtility = new TransferUtility(client); // Use TransferUtilityUploadRequest to configure options. // In this example we subscribe to an event. var uploadRequest = new TransferUtilityUploadRequest { BucketName = bucketName, FilePath = filePath, Key = keyName, }; uploadRequest.UploadProgressEvent += new EventHandler<UploadProgressArgs>( UploadRequest_UploadPartProgressEvent); await fileTransferUtility.UploadAsync(uploadRequest); Console.WriteLine("Upload completed"); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error:: {ex.Message}"); } } /// <summary> /// Event handler to check the progress of the multipart upload. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="e">The object that contains multipart upload /// information.</param> public static void UploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e) { // Process event. Console.WriteLine($"{e.TransferredBytes}/{e.TotalBytes}"); } }

Upload an object with encryption.

using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; public class SSECLowLevelMPUcopyObject { public static async Task Main() { string existingBucketName = "doc-example-bucket"; string sourceKeyName = "sample_file.txt"; string targetKeyName = "sample_file_copy.txt"; string filePath = $"sample\\{targetKeyName}"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the Amazon S3 client object's constructor. // For example: RegionEndpoint.USEast1. IAmazonS3 client = new AmazonS3Client(); // Create the encryption key. var base64Key = CreateEncryptionKey(); await CreateSampleObjUsingClientEncryptionKeyAsync( client, existingBucketName, sourceKeyName, filePath, base64Key); } /// <summary> /// Creates the encryption key to use with the multipart upload. /// </summary> /// <returns>A string containing the base64-encoded key for encrypting /// the multipart upload.</returns> public static string CreateEncryptionKey() { Aes aesEncryption = Aes.Create(); aesEncryption.KeySize = 256; aesEncryption.GenerateKey(); string base64Key = Convert.ToBase64String(aesEncryption.Key); return base64Key; } /// <summary> /// Creates and uploads an object using a multipart upload. /// </summary> /// <param name="client">The initialized Amazon S3 object used to /// initialize and perform the multipart upload.</param> /// <param name="existingBucketName">The name of the bucket to which /// the object will be uploaded.</param> /// <param name="sourceKeyName">The source object name.</param> /// <param name="filePath">The location of the source object.</param> /// <param name="base64Key">The encryption key to use with the upload.</param> public static async Task CreateSampleObjUsingClientEncryptionKeyAsync( IAmazonS3 client, string existingBucketName, string sourceKeyName, string filePath, string base64Key) { List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>(); InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = existingBucketName, Key = sourceKeyName, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key, }; InitiateMultipartUploadResponse initResponse = await client.InitiateMultipartUploadAsync(initiateRequest); long contentLength = new FileInfo(filePath).Length; long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB try { long filePosition = 0; for (int i = 1; filePosition < contentLength; i++) { UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = existingBucketName, Key = sourceKeyName, UploadId = initResponse.UploadId, PartNumber = i, PartSize = partSize, FilePosition = filePosition, FilePath = filePath, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.AES256, ServerSideEncryptionCustomerProvidedKey = base64Key, }; // Upload part and add response to our list. uploadResponses.Add(await client.UploadPartAsync(uploadRequest)); filePosition += partSize; } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = existingBucketName, Key = sourceKeyName, UploadId = initResponse.UploadId, }; completeRequest.AddPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = await client.CompleteMultipartUploadAsync(completeRequest); } catch (Exception exception) { Console.WriteLine($"Exception occurred: {exception.Message}"); // If there was an error, abort the multipart upload. AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest { BucketName = existingBucketName, Key = sourceKeyName, UploadId = initResponse.UploadId, }; await client.AbortMultipartUploadAsync(abortMPURequest); } } }