AmazonS3EncryptionClientV2 - Unable to find a region via the region provider chain

Viewed 320

I have to migrate from AmazonS3EncryptionClient to AmazonS3EncryptionClientV2, therefore I was following these instructions: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/s3-encryption-migration.html, but I'm always running into the same error, even though the region is set: "Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region."

AmazonS3 s3Encryption = AmazonS3EncryptionClientV2.encryptionBuilder()
                    .withRegion(Regions.EU_CENTRAL_1)
                    .withCredentials(new AWSStaticCredentialsProvider(
                            new BasicAWSCredentials(AWS_S3_KEY, AWS_S3_SECRET)))
                    .withCryptoConfiguration(new CryptoConfigurationV2()
                            // The following setting allows the client to read V1 encrypted objects
                            .withCryptoMode(CryptoMode.AuthenticatedEncryption)
                            .withCryptoProvider(new BouncyCastleProvider()))
                    .withEncryptionMaterialsProvider(new StaticEncryptionMaterialsProvider(
                            new EncryptionMaterials(createKeyPair(AWS_S3_PRIVATE_KEY,
                                    AWS_S3_PUBLIC_KEY))))
                    .build();

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-s3</artifactId>
  <version>1.11.908</version>
</dependency>

Has anyone of an idea? Because when I use another client like the AmazonS3ClientBuilder everything works fine.

3 Answers

Try this

val kmsClient:AWSKMS = AWSKMSClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
    

val s3Client:AmazonS3EncryptionV2 = AmazonS3EncryptionClientV2Builder.standard()
  .withKmsClient(kmsClient)
  .withRegion(Regions.US_EAST_1)
  .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.StrictAuthenticatedEncryption)))
  .withEncryptionMaterialsProvider(materialProvider)
  .build()

You just have to specify the region in CryptoConfigurationV2: .withAwsKmsRegion(Region.getRegion(Regions.AP_SOUTH_1))

Eg:

.withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode(CryptoMode.AuthenticatedEncryptionn).withCryptoProvider(new BouncyCastleProvider()).withAwsKmsRegion(Region.getRegion(Regions.AP_SOUTH_1)))

Faced the same issue and resolved it by creating a config file

~/.aws/config

[default]

region=us-east-2

Related