GetBucketEncryption 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GetBucketEncryption 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 GetBucketEncryption

.NET
AWS SDK for .NET
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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

擷取儲存貯體的伺服器端加密組態

下列get-bucket-encryption範例會擷取儲存貯體 的伺服器端加密組態my-bucket

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

輸出:

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

範例 1:此命令會傳回與指定儲存貯體相關聯的所有伺服器端加密規則。

Get-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket'
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 GetBucketEncryption