繁体   English   中英

AWS S3 KMS Java:提供属性文件 MultiLangDaemon

[英]AWS S3 KMS Java: provide properties file MultiLangDaemon

我想创建一个示例代码来将加密文件上传和下载到 AWS S3。 当我尝试运行代码时,出现以下错误:

您必须提供一个属性文件java com.amazonaws.services.kinesis.multilang.MultiLangDaemon

我不知道,为什么这里会影响运动。 即使我在项目目录中创建了一个名为 config.properties 的文件,它也无法识别。

我的代码:

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 显示代码没有错误或警告。 我将 Eclipse IDE 用于 Java 开发人员 2021-12 (4.22.0)。 AWS 账户具有(用于测试)以下权利:

  • AmazonS3FullAccess
  • CloudWatchFullAccess
  • AmazonDynamoDBFullAccess
  • AmazonKinesisFullAccess
  • AWSKeyManagementServicePowerUser

我的.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>

我是一个绝对的初学者。 因此,如果您对代码有其他改进(除了错误的修复),请告诉我。 提前致谢!

你得到的错误是由于JDK中的一些变化,你必须将JDK--version升级到11或更新的更新版本,jdk-8不能支持一些新功能。 必须添加以下依赖项:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM