Home > Back-end >  AWS S3 KMS Java: provide properties file MultiLangDaemon
AWS S3 KMS Java: provide properties file MultiLangDaemon

Time:01-31

I want to create an example code to upload and download a encrypted file to AWS S3. When I try to run the code, I get the following error:

You must provide a properties filejava com.amazonaws.services.kinesis.multilang.MultiLangDaemon

I don´t know, why kinesis is affected here. Even when I create a file named config.properties in project directory, it isn´t recognized.

My code:

package com.amazonaws.samples;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.kms.AWSKMS;
import com.amazonaws.services.kms.AWSKMSClientBuilder;
import com.amazonaws.services.kms.model.CreateKeyRequest;
import com.amazonaws.services.kms.model.CreateKeyResult;
import com.amazonaws.services.kms.model.ScheduleKeyDeletionRequest;
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.KMSEncryptionMaterialsProvider;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.waiters.S3Waiter;

public class S3Sample_encrypted_kms {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        
        //KinesisProducerConfiguration.fromPropertiesFile(String)
                
        // Specifies the AWS Region
        Region region = Region.US_WEST_2;

        // Set AWS Credentials
        AwsBasicCredentials awsCreds = AwsBasicCredentials.create("KEY",
                "SECRET");

        S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(awsCreds)).region(region)
                .build();

        String bucketName = "aws-test-bucket-"   UUID.randomUUID();

        System.out.println("===========================================");
        System.out.println("Getting Started with Amazon S3");
        System.out.println("===========================================\n");

        try {

            /*
             * Create a new S3 bucket - Amazon S3 bucket names are globally unique, so once
             * a bucket name has been taken by any user, you can't create another bucket
             * with that same name. Because of this, a randomUUID is used.
             */
            System.out.println("Creating bucket "   bucketName   "\n");

            // Details see method at the end of the file
            createBucket(s3, bucketName);

            // Name of the Object
            String s3ObjectKey = "mockdata";

            // Encryption Initialization
            AWSKMS kmsClient = AWSKMSClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();

            // create KMS key for for testing this example
            CreateKeyRequest createKeyRequest = new CreateKeyRequest();
            CreateKeyResult createKeyResult = kmsClient.createKey(createKeyRequest);

            // --
            // specify an AWS KMS key ID
            String keyId = createKeyResult.getKeyMetadata().getKeyId();
            // --
            
            AmazonS3EncryptionV2 s3Encryption = AmazonS3EncryptionClientV2Builder.standard()
                     .withRegion(Regions.US_WEST_2)
                     .withCryptoConfiguration(new CryptoConfigurationV2().withCryptoMode((CryptoMode.StrictAuthenticatedEncryption)))
                     .withEncryptionMaterialsProvider(new KMSEncryptionMaterialsProvider(keyId))
                     .build();

            // File upload
            s3Encryption.putObject(bucketName, s3ObjectKey, new File("./data/MOCK_DATA.csv"));

            // File download and display
            System.out.println(s3Encryption.getObjectAsString(bucketName, s3ObjectKey));

            // schedule deletion of KMS key generated for testing
            ScheduleKeyDeletionRequest scheduleKeyDeletionRequest = new ScheduleKeyDeletionRequest().withKeyId(keyId)
                    .withPendingWindowInDays(7);
            kmsClient.scheduleKeyDeletion(scheduleKeyDeletionRequest);

            // Close Encryption Session
            s3Encryption.shutdown();
            kmsClient.shutdown();
            System.out.println("Encryption Session closed");

        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which means your request made it "
                      "to Amazon S3, but was rejected with an error response for some reason.");
            System.out.println("Error Message:    "   ase.getMessage());
            System.out.println("HTTP Status Code: "   ase.getStatusCode());
            System.out.println("AWS Error Code:   "   ase.getErrorCode());
            System.out.println("Error Type:       "   ase.getErrorType());
            System.out.println("Request ID:       "   ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means the client encountered "
                      "a serious internal problem while trying to communicate with S3, "
                      "such as not being able to access the network.");
            System.out.println("Error Message: "   ace.getMessage());
        }
    }

    // Create a bucket by using a S3Waiter object
    public static void createBucket(S3Client s3Client, String bucketName) {

        try {
            S3Waiter s3Waiter = s3Client.waiter();
            CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();

            s3Client.createBucket(bucketRequest);
            HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();

            // Wait until the bucket is created and print out the response
            WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
            waiterResponse.matched().response().ifPresent(System.out::println);
            System.out.println(bucketName   " is ready");

        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
    
}

Eclipse shows for the code no errors or warnings. I use Eclipse IDE for Java Developers 2021-12 (4.22.0). The AWS account has (for testing) following rights:

  • AmazonS3FullAccess
  • CloudWatchFullAccess
  • AmazonDynamoDBFullAccess
  • AmazonKinesisFullAccess
  • AWSKeyManagementServicePowerUser

My .pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.amazonaws</groupId>
    <artifactId>samples</artifactId>
    <version>1.0.0</version>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.12.146</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>amazon-kinesis-client</artifactId>
            <version>1.14.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>software.amazon.kinesis</groupId>
            <artifactId>amazon-kinesis-client</artifactId>
            <version>2.3.5</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.kinesis</groupId>
            <artifactId>amazon-kinesis-client-multilang</artifactId>
            <version>2.3.10</version>
        </dependency>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>auth</artifactId>
            <version>2.17.117</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>2.17.117</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.33</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.33</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

    </dependencies>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I am an absolute beginner. So if you have (beside the fix for the error) other improvements for the code, let me know. Thanks in advance!

CodePudding user response:

The error you are getting is due to some changes in the JDK, you must upgrade the JDK--version to 11 or a newer updated version, the jdk-8 cant support some new features. The following dependencies have to be added:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.33</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.33</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
  •  Tags:  
  • Related