与 AWS SDK或PutBucketEncryption一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或PutBucketEncryption一起使用 CLI

以下代码示例演示如何使用 PutBucketEncryption

.NET
AWS SDK for .NET
注意

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

/// <summary> /// Set the bucket server side encryption to use AWSKMS with a customer-managed key id. /// </summary> /// <param name="bucketName">Name of the bucket.</param> /// <param name="kmsKeyId">The Id of the KMS Key.</param> /// <returns>True if successful.</returns> public static async Task<bool> SetBucketServerSideEncryption(string bucketName, string kmsKeyId) { var serverSideEncryptionByDefault = new ServerSideEncryptionConfiguration { ServerSideEncryptionRules = new List<ServerSideEncryptionRule> { new ServerSideEncryptionRule { ServerSideEncryptionByDefault = new ServerSideEncryptionByDefault { ServerSideEncryptionAlgorithm = ServerSideEncryptionMethod.AWSKMS, ServerSideEncryptionKeyManagementServiceKeyId = kmsKeyId } } } }; try { var encryptionResponse = await _s3Client.PutBucketEncryptionAsync(new PutBucketEncryptionRequest { BucketName = bucketName, ServerSideEncryptionConfiguration = serverSideEncryptionByDefault, }); return encryptionResponse.HttpStatusCode == HttpStatusCode.OK; } catch (AmazonS3Exception ex) { Console.WriteLine(ex.ErrorCode == "AccessDenied" ? $"This account does not have permission to set encryption on {bucketName}, please try again." : $"Unable to set bucket encryption for bucket {bucketName}, {ex.Message}"); } return false; }
  • 有关API详细信息,请参阅 “AWS SDK for .NET API参考 PutBucketEncryption” 中的。

CLI
AWS CLI

配置存储桶的服务器端加密

以下put-bucket-encryption示例将AES256加密设置为指定存储桶的默认值。

aws s3api put-bucket-encryption \ --bucket my-bucket \ --server-side-encryption-configuration '{"Rules": [{"ApplyServerSideEncryptionByDefault": {"SSEAlgorithm": "AES256"}}]}'

此命令不生成任何输出。

PowerShell
用于 PowerShell

示例 1:此命令使用给定存储桶上的 Amazon S3 托管密钥 (SSE-S3) 启用默认AES256服务器端加密。

$Encryptionconfig = @{ServerSideEncryptionByDefault = @{ServerSideEncryptionAlgorithm = "AES256"}} Set-S3BucketEncryption -BucketName 'amzn-s3-demo-bucket' -ServerSideEncryptionConfiguration_ServerSideEncryptionRule $Encryptionconfig
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考PutBucketEncryption中的。