Use GetBucketEncryption with an AWS SDK or CLI - Amazon Simple Storage Service

Use GetBucketEncryption with an AWS SDK or CLI

The following code examples show how to use GetBucketEncryption.

.NET
AWS SDK for .NET
Note

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

/// <summary> /// Get and print the encryption settings of a bucket. /// </summary> /// <param name="bucketName">Name of the bucket.</param> /// <returns>Async task.</returns> public static async Task GetEncryptionSettings(string bucketName) { // Check and print the bucket encryption settings. Console.WriteLine($"Getting encryption settings for bucket {bucketName}."); try { var settings = await _s3Client.GetBucketEncryptionAsync( new GetBucketEncryptionRequest() { BucketName = bucketName }); foreach (var encryptionSettings in settings?.ServerSideEncryptionConfiguration?.ServerSideEncryptionRules!) { Console.WriteLine( $"\tAlgorithm: {encryptionSettings.ServerSideEncryptionByDefault.ServerSideEncryptionAlgorithm}"); Console.WriteLine( $"\tKey: {encryptionSettings.ServerSideEncryptionByDefault.ServerSideEncryptionKeyManagementServiceKeyId}"); } } catch (AmazonS3Exception ex) { Console.WriteLine(ex.ErrorCode == "InvalidBucketName" ? $"Bucket {bucketName} was not found." : $"Unable to get bucket encryption for bucket {bucketName}, {ex.Message}"); } }
CLI
AWS CLI

To retrieve the server-side encryption configuration for a bucket

The following get-bucket-encryption example retrieves the server-side encryption configuration for the bucket my-bucket.

aws s3api get-bucket-encryption \ --bucket my-bucket

Output:

{ "ServerSideEncryptionConfiguration": { "Rules": [ { "ApplyServerSideEncryptionByDefault": { "SSEAlgorithm": "AES256" } } ] } }
PowerShell
Tools for PowerShell

Example 1: This command returns all the server side encryption rules associated with the given bucket.

Get-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket'

For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.