使用 Amazon SDK for .NET 的 Amazon S3 示例 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SDK for .NET 的 Amazon S3 示例

以下代码示例演示了如何将Amazon SDK for .NET 与 Amazon S3 结合使用,以执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是指显示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

操作

以下代码示例演示了如何向 S3 存储桶添加跨源资源共享 (CORS) 规则。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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 PutCORSConfigurationRequest() { BucketName = BucketName, Configuration = configuration, }; _ = await client.PutCORSConfigurationAsync(request); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutBucketCors中的。

以下代码示例演示如何将生命周期配置添加到 S3 桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

以下代码示例说明如何取消分段上传。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; /// <summary> /// This example shows how to use the Amazon Simple Storage Service /// (Amazon S3) to stop a multi-part upload process using the Amazon S3 /// TransferUtility. /// </summary> 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}"); } } }

以下代码示例演示了如何将 S3 对象从一个桶复制到另一个桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; 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; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CopyObject中的。

以下代码示例演示了如何创建 S3 存储桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考CreateBucket中的。

以下代码示例演示如何从 S3 桶中删除 CORS 规则。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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 DeleteCORSConfigurationRequest() { BucketName = BucketName, }; await client.DeleteCORSConfigurationAsync(request); }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteBucketCors中的。

以下代码示例演示了如何删除空的 S3 存储桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteBucket中的。

以下代码示例演示了如何删除 S3 对象。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

删除不受版本控制的 S3 桶中的对象。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to delete an object from a non-versioned Amazon /// Simple Storage Service (Amazon S3) bucket. /// </summary> 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."); } } }

删除受版本控制的 S3 桶中的对象。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example creates an object in an Amazon Simple Storage Service /// (Amazon S3) bucket and then deletes the object version that was /// created. /// </summary> 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 DeleteObjectRequest() { 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 PutObjectRequest() { BucketName = bucketName, Key = objectKey, ContentBody = "This is the content body!", }; PutObjectResponse response = await client.PutObjectAsync(request); return response.VersionId; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteObject中的。

以下代码示例演示了如何从 S3 存储桶中删除多个对象。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

从 S3 桶中删除所有对象。

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

删除不受版本控制的 S3 桶中的多个对象。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to delete multiple objects from an Amazon Simple /// Storage Service (Amazon S3) bucket. /// </summary> 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; } }

删除受版本控制的 S3 桶中的多个对象。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to delete objects in a version-enabled Amazon /// Simple StorageService (Amazon S3) bucket. /// </summary> 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> /// <param name="client">The S3 client.</param> /// <param name="bucketName">Name of the bucket.</param> /// <returns>Async task.</returns> 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> private 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> private 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> private 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; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考DeleteObjects中的。

以下代码示例显示如何删除 S3 桶的生命周期配置。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

以下代码示例演示了如何使用在 S3 桶上启用日志记录。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.IO; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; using Microsoft.Extensions.Configuration; /// <summary> /// This example shows how to enable logging on an Amazon Simple Storage /// Service (Amazon S3) bucket. You need to have two Amazon S3 buckets for /// this example. The first is the bucket for which you wish to enable /// logging, and the second is the location where you want to store the /// logs. /// </summary> public class ServerAccessLogging { private static IConfiguration _configuration = null!; public static async Task Main() { LoadConfig(); string bucketName = _configuration["BucketName"]; string logBucketName = _configuration["LogBucketName"]; string logObjectKeyPrefix = _configuration["LogObjectKeyPrefix"]; string accountId = _configuration["AccountId"]; // 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 { // Update bucket policy for target bucket to allow delivery of logs to it. await SetBucketPolicyToAllowLogDelivery( client, bucketName, logBucketName, logObjectKeyPrefix, accountId); // 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 apply the bucket policy.</param> /// <param name="sourceBucketName">The name of the source bucket.</param> /// <param name="logBucketName">The name of the bucket where logging /// information will be stored.</param> /// <param name="logPrefix">The logging prefix where the logs should be delivered.</param> /// <param name="accountId">The account id of the account where the source bucket exists.</param> /// <returns>Async task.</returns> public static async Task SetBucketPolicyToAllowLogDelivery( IAmazonS3 client, string sourceBucketName, string logBucketName, string logPrefix, string accountId) { var resourceArn = @"""arn:aws:s3:::" + logBucketName + "/" + logPrefix + @"*"""; var newPolicy = @"{ ""Statement"":[{ ""Sid"": ""S3ServerAccessLogsPolicy"", ""Effect"": ""Allow"", ""Principal"": { ""Service"": ""logging.s3.amazonaws.com"" }, ""Action"": [""s3:PutObject""], ""Resource"": [" + resourceArn + @"], ""Condition"": { ""ArnLike"": { ""aws:SourceArn"": ""arn:aws:s3:::" + sourceBucketName + @""" }, ""StringEquals"": { ""aws:SourceAccount"": """ + accountId + @""" } } }] }"; Console.WriteLine($"The policy to apply to bucket {logBucketName} to enable logging:"); Console.WriteLine(newPolicy); PutBucketPolicyRequest putRequest = new PutBucketPolicyRequest { BucketName = logBucketName, Policy = newPolicy, }; await client.PutBucketPolicyAsync(putRequest); Console.WriteLine("Policy applied."); } /// <summary> /// This method enables logging for an Amazon S3 bucket. Logs will be stored /// in the bucket you selected for logging. Selected prefix /// will be prepended to each log object. /// </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> /// <returns>Async task.</returns> public static async Task EnableLoggingAsync( IAmazonS3 client, string bucketName, string logBucketName, string logObjectKeyPrefix) { Console.WriteLine($"Enabling logging for bucket {bucketName}."); var loggingConfig = new S3BucketLoggingConfig { TargetBucketName = logBucketName, TargetPrefix = logObjectKeyPrefix, }; var putBucketLoggingRequest = new PutBucketLoggingRequest { BucketName = bucketName, LoggingConfig = loggingConfig, }; await client.PutBucketLoggingAsync(putBucketLoggingRequest); Console.WriteLine($"Logging enabled."); } /// <summary> /// Loads configuration from settings files. /// </summary> public static void LoadConfig() { _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally, load local settings. .Build(); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutBucketLogging中的。

下面的代码示例显示如何对 S3 桶启用通知。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to enable notifications for an Amazon Simple /// Storage Service (Amazon S3) bucket. /// </summary> 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}"); } } }

以下代码示例显示如何对 S3 桶启用传输加速。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// Amazon Simple Storage Service (Amazon S3) Transfer Acceleration is a /// bucket-level feature that enables you to perform faster data transfers /// to Amazon S3. This example shows how to configure Transfer /// Acceleration. /// </summary> public 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> private 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"); } } }

以下代码示例演示了如何获取 S3 存储桶的跨源资源共享 (CORS) 规则。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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 GetCORSConfigurationRequest() { BucketName = BucketName, }; var response = await client.GetCORSConfigurationAsync(request); var configuration = response.Configuration; PrintCORSRules(configuration); return configuration; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetBucketCors中的。

以下代码示例演示了如何从 S3 存储桶中的对象读取数据。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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; } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetObject中的。

以下代码示例演示了如何获取 S3 存储桶的访问控制列表 (ACL)。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetBucketAcl中的。

以下代码示例显示如何获取 S3 桶的生命周期配置。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

以下代码示例演示了如何获取 S3 存储桶的网站配置。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// 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}");
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetBucketWebsite中的。

以下代码示例演示了如何列出 S3 存储桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

namespace ListBucketsExample { using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example uses the AWS SDK for .NET to list the Amazon Simple Storage /// Service (Amazon S3) buckets belonging to the default account. /// </summary> public class ListBuckets { private static IAmazonS3 _s3Client; /// <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}")); } public 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); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListBuckets中的。

以下代码示例显示如何列出 S3 桶中的对象版本。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example lists the versions of the objects in a version enabled /// Amazon Simple Storage Service (Amazon S3) bucket. /// </summary> 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 ListVersionsRequest() { 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}'"); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考ListObjectVersions中的。

以下代码示例演示了如何列出 S3 存储桶中的对象。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

使用分页工具列出对象。

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. /// </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}"); } } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考中的 ListObjectsV2

以下代码示例演示如何将对象的存档副本还原到 S3 桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to restore an archived object in an Amazon /// Simple Storage Service (Amazon S3) bucket. /// </summary> 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 GetObjectMetadataRequest() { BucketName = bucketName, Key = objectKey, }; GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest); var restStatus = response.RestoreInProgress ? "in-progress" : "finished or failed"; Console.WriteLine($"Restoration status: {restStatus}"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考RestoreObject中的。

以下代码示例演示了如何为 S3 存储桶设置新的访问控制列表 (ACL)。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// <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; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutBucketAcl中的。

以下示例演示了如何设置 S3 存储桶的网站配置。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// Put the website configuration. PutBucketWebsiteRequest putRequest = new PutBucketWebsiteRequest() { BucketName = bucketName, WebsiteConfiguration = new WebsiteConfiguration() { IndexDocumentSuffix = indexDocumentSuffix, ErrorDocument = errorDocument, }, }; PutBucketWebsiteResponse response = await client.PutBucketWebsiteAsync(putRequest);
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutBucketWebsite中的。

以下代码示例演示了如何将对象上传到 S3 存储桶。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

使用服务器端加密上载对象。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to upload an object to an Amazon Simple Storage /// Service (Amazon S3) bucket with server-side encryption enabled. /// </summary> 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"); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考PutObject中的。

场景

以下代码示例演示了如何为 Amazon S3 创建预签名 URL 以及如何上传对象。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

生成可在有限时间内执行 Amazon S3 操作的预签名 URL。

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: new AmazonS3Client(RegionEndpoint.USEast1); // If using the Region us-east-1, and server-side encryption with AWS KMS, you must specify Signature Version 4. // Region us-east-1 defaults to Signature Version 2 unless explicitly set to Version 4 as shown below. // For more details, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAWSSDK.html#specify-signature-version // and https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Amazon/TAWSConfigsS3.html AWSConfigsS3.UseSignatureVersion4 = true; IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USEast1); 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; } }

生成预签名 URL 并使用该 URL 执行上载。

using System; using System.IO; using System.Net.Http; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to upload an object to an Amazon Simple Storage /// Service (Amazon S3) bucket using a presigned URL. The code first /// creates a presigned URL and then uses it to upload an object to an /// Amazon S3 bucket using that URL. /// </summary> public class UploadUsingPresignedURL { private static HttpClient httpClient = new HttpClient(); public static async Task 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; // 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: new AmazonS3Client(RegionEndpoint.USEast1); // If using the Region us-east-1, and server-side encryption with AWS KMS, you must specify Signature Version 4. // Region us-east-1 defaults to Signature Version 2 unless explicitly set to Version 4 as shown below. // For more details, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAWSSDK.html#specify-signature-version // and https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Amazon/TAWSConfigsS3.html AWSConfigsS3.UseSignatureVersion4 = true; IAmazonS3 client = new AmazonS3Client(RegionEndpoint.USEast1); var url = GeneratePreSignedURL(client, bucketName, keyName, timeoutDuration); var success = await 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 async Task<bool> UploadObject(string filePath, string url) { using var streamContent = new StreamContent( new FileStream(filePath, FileMode.Open, FileAccess.Read)); var response = await httpClient.PutAsync(url, streamContent); return response.IsSuccessStatusCode; } /// <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; } }

以下代码示例显示了操作流程:

  • 创建存储桶并上传文件。

  • 下载存储桶中的对象。

  • 复制对象到存储桶中的子文件夹。

  • 列出存储桶中的对象。

  • 删除存储桶及其对象。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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

下面的代码示例显示如何开始 Amazon S3 对象的加密。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.IO; using System.Security.Cryptography; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to apply client encryption to an object in an /// Amazon Simple Storage Service (Amazon S3) bucket. /// </summary> 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); } }

以下代码示例演示如何开始为 Amazon S3 对象加标签。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to work with tags in Amazon Simple Storage /// Service (Amazon S3) objects. /// </summary> 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 GetObjectTaggingRequest() { 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 Tagging() { TagSet = new List<Tag> { new Tag { Key = "Key3", Value = "Value3" }, new Tag { Key = "Key4", Value = "Value4" }, }, }; PutObjectTaggingRequest putObjTagsRequest = new PutObjectTaggingRequest() { BucketName = bucketName, Key = keyName, Tagging = newTagSet, }; PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest); // Retrieve the tags again and show the values. GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest() { 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}'"); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NETAPI 参考GetObjectTagging中的。

以下代码示例显示如何管理 Amazon S3 桶的访问控制列表(ACL)。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to manage Amazon Simple Storage Service /// (Amazon S3) access control lists (ACLs) to control Amazon S3 bucket /// access. /// </summary> 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.EUWest1, // 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, }); } }

以下代码示例显示如何执行 Amazon S3 对象的分段复制。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to perform a multi-part copy from one Amazon /// Simple Storage Service (Amazon S3) bucket to another. /// </summary> 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"); } } }

以下代码示例演示了如何向 Amazon S3 上传大文件或从 Amazon S3 下载大文件。

有关更多信息,请参阅使用分段上传操作上传对象

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

调用使用 Amazon S3 将文件传输到 S3 存储桶和传出 S3 存储桶的函数 TransferUtility。

global using System.Text; 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); }

上载单个文件。

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

上载整个本地目录。

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

下载单个文件。

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

下载 S3 桶的内容。

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

使用跟踪上传进度 TransferUtility。

using System; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Transfer; /// <summary> /// This example shows how to track the progress of a multipart upload /// using the Amazon Simple Storage Service (Amazon S3) TransferUtility to /// upload to an Amazon S3 bucket. /// </summary> 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}"); } }

上载经过加密的对象。

using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// Uses the Amazon Simple Storage Service (Amazon S3) low level API to /// perform a multipart upload to an Amazon S3 bucket. /// </summary> 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); } } }

无服务器示例

以下代码示例演示如何实现一个 Lambda 函数,该函数接收将对象上传到 S3 桶时触发的事件。该函数从事件参数中检索 S3 桶名称和对象密钥,并调用 Amazon S3 API 来检索和记录对象的内容类型。

Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在无服务器示例存储库中查找完整示例,并了解如何进行设置和运行。

使用 .NET 将 S3 事件与 Lambda 结合使用。

using System.Threading.Tasks; using Amazon.Lambda.Core; using Amazon.S3; using System; using Amazon.Lambda.S3Events; using System.Web; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace S3Integration { public class Function { private static AmazonS3Client _s3Client; public Function() : this(null) { } internal Function(AmazonS3Client s3Client) { _s3Client = s3Client ?? new AmazonS3Client(); } public async Task<string> Handler(S3Event evt, ILambdaContext context) { try { if (evt.Records.Count <= 0) { context.Logger.LogLine("Empty S3 Event received"); return string.Empty; } var bucket = evt.Records[0].S3.Bucket.Name; var key = HttpUtility.UrlDecode(evt.Records[0].S3.Object.Key); context.Logger.LogLine($"Request is for {bucket} and {key}"); var objectResult = await _s3Client.GetObjectAsync(bucket, key); context.Logger.LogLine($"Returning {objectResult.Key}"); return objectResult.Key; } catch (Exception e) { context.Logger.LogLine($"Error processing request - {e.Message}"); return string.Empty; } } } }