自2024年7月31日起, AWS SDK for Java 1.x已进入维护模式,并将于2025年12月31日end-of-support
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon S3 使用 AWS KMS托管密钥进行客户端加密
以下示例使用 AmazonS3 EncryptionClient V2Builder 类创建启用了 Amazon S3 客户端加密的客户端。配置完成后,您 Amazon S3 使用此客户端上传到的任何对象都将被加密。您 Amazon S3 使用此客户端获得的任何对象都会自动解密。
注意
以下示例演示如何对 AWS KMS托管密钥使用 Amazon S3 客户端加密。要了解如何将加密与您自己的密钥配合使用,请参阅 Amazon S3 客户端加密配合客户端主密钥。
启用客户端 Amazon S3 加密时,您可以从两种加密模式中进行选择:严格认证或经过身份验证。以下部分说明了如何启用每种类型。要了解每种模式使用哪些算法,请参阅CryptoMode定义。
必需的导入
为这些示例导入以下类。
导入
import com.amazonaws.ClientConfiguration; import com.amazonaws.regions.Regions; import com.amazonaws.services.kms.AWSKMS; import com.amazonaws.services.kms.AWSKMSClientBuilder; import com.amazonaws.services.kms.model.GenerateDataKeyRequest; import com.amazonaws.services.kms.model.GenerateDataKeyResult; import com.amazonaws.services.s3.AmazonS3EncryptionClientV2Builder; import com.amazonaws.services.s3.AmazonS3EncryptionV2; import com.amazonaws.services.s3.model.CryptoConfigurationV2; import com.amazonaws.services.s3.model.CryptoMode; import com.amazonaws.services.s3.model.EncryptionMaterials; import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider;
经严格身份验证加密
如果未指定 CryptoMode
,则默认模式为经严格身份验证加密。
要显式启用此模式,请在 withCryptoConfiguration
方法中指定 StrictAuthenticatedEncryption
值。
注意
要使用客户端经身份验证加密,您必须将最新的 Bouncy Castle jar
代码
AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.StrictAuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build(); s3Encryption.putObject(bucket_name, ENCRYPTED_KEY3, "This is the 3rd content to encrypt with a key created in the {console}"); System.out.println(s3Encryption.getObjectAsString(bucket_name, ENCRYPTED_KEY3));
在 Amazon S3 加密客户端上调用该putObject
方法上传对象。
代码
s3Encryption.putObject(bucket_name, ENCRYPTED_KEY3, "This is the 3rd content to encrypt with a key created in the {console}");
您可以使用同一个客户端检索该对象。此示例调用 getObjectAsString
方法以检索存储的字符串。
代码
System.out.println(s3Encryption.getObjectAsString(bucket_name, ENCRYPTED_KEY3));
经身份验证加密模式
使用 AuthenticatedEncryption
模式时,在加密期间会应用改进的密钥包装算法。在此模式下解密时,该算法会验证已解密对象的完整性,如果检查失败,则引发异常。有关经身份验证加密模式工作原理的更多详细信息,请参阅博客文章 Amazon S3 Client-Side Authenticated Encryption
注意
要使用客户端经身份验证加密,您必须将最新的 Bouncy Castle jar
要启用此模式,请在 withCryptoConfiguration
方法中指定 AuthenticatedEncryption
值。
代码
AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.AuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();
配置 AWS KMS 客户端
除非明确指定客户 AWS KMS 端,否则默认情况下, Amazon S3 加密客户端会创建一个客户端。
要为这个自动创建的 AWS KMS 客户端设置区域,请设置。awsKmsRegion
代码
Region kmsRegion = Region.getRegion(Regions.AP_NORTHEAST_1); AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfigurationV2().withAwsKmsRegion(kmsRegion)) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();
或者,您可以使用自己的 AWS KMS 客户端来初始化加密客户端。
代码
AWSKMS kmsClient = AWSKMSClientBuilder.standard() .withRegion(Regions.US_WEST_2); .build(); AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard() .withRegion(Regions.US_WEST_2) .withKmsClient(kmsClient) .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.AuthenticatedEncryption))) .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId)) .build();