使用 aws-java-sdk-s3 时出错

Posted

技术标签:

【中文标题】使用 aws-java-sdk-s3 时出错【英文标题】:Error in using aws-java-sdk-s3 【发布时间】:2018-08-11 14:41:09 【问题描述】:

我正在尝试通过非常基本的测试来 s3 sdk 并得到以下错误。

原因:java.lang.NoSuchFieldError: SIGNING_REGION 在 com.amazonaws.services.s3.AmazonS3Client.createRequest(AmazonS3Client.java:4227) 在 com.amazonaws.services.s3.AmazonS3Client.createRequest(AmazonS3Client.java:4203) 在 com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:929) 在 com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:936)

aws-java-sdk-s3

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

    ClientConfiguration cf = new ClientConfiguration();
    AWSCredentials credentials = new BasicAWSCredentials("<id>","<secret>");
    AmazonS3 amazonS3Client= 
    AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.US_EAST_1).build();
    List<Bucket> buckets = amazonS3Client.listBuckets();

Maven 依赖树:

O] --- maven-dependency-plugin:2.10:tree (default-cli) @ AwsSdkDemo ---
O] com.example:AwsSdkDemo:jar:0.0.1-SNAPSHOT
O] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.5.10.RELEASE:compile
O] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.10.RELEASE:compile
O] |  |  +- org.springframework.boot:spring-boot:jar:1.5.10.RELEASE:compile
O] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.10.RELEASE:compile
O] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.10.RELEASE:compile
O] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
O] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.11:compile
O] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
O] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
O] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
O] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.5.10.RELEASE:compile
O] |  |  +- org.springframework:spring-aop:jar:4.3.14.RELEASE:compile
O] |  |  \- org.aspectj:aspectjweaver:jar:1.8.13:compile
O] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.10.RELEASE:compile
O] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.27:compile
O] |  |  |  \- org.apache.tomcat:tomcat-juli:jar:8.5.27:compile
O] |  |  \- org.springframework:spring-jdbc:jar:4.3.14.RELEASE:compile
O] |  +- org.hibernate:hibernate-core:jar:5.0.12.Final:compile
O] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
O] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
O] |  |  +- org.javassist:javassist:jar:3.21.0-GA:compile
O] |  |  +- antlr:antlr:jar:2.7.7:compile
O] |  |  +- org.jboss:jandex:jar:2.0.0.Final:compile
O] |  |  +- dom4j:dom4j:jar:1.6.1:compile
O] |  |  \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
O] |  +- org.hibernate:hibernate-entitymanager:jar:5.0.12.Final:compile
O] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
O] |  +- org.springframework.data:spring-data-jpa:jar:1.11.10.RELEASE:compile
O] |  |  +- org.springframework.data:spring-data-commons:jar:1.13.10.RELEASE:compile
O] |  |  +- org.springframework:spring-orm:jar:4.3.14.RELEASE:compile
O] |  |  +- org.springframework:spring-context:jar:4.3.14.RELEASE:compile
O] |  |  +- org.springframework:spring-tx:jar:4.3.14.RELEASE:compile
O] |  |  +- org.springframework:spring-beans:jar:4.3.14.RELEASE:compile
O] |  |  +- org.slf4j:slf4j-api:jar:1.7.25:compile
O] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
O] |  \- org.springframework:spring-aspects:jar:4.3.14.RELEASE:compile
O] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.10.RELEASE:compile
O] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.10.RELEASE:compile
O] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.27:compile
O] |  |  |  \- org.apache.tomcat:tomcat-annotations-api:jar:8.5.27:compile
O] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.27:compile
O] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.27:compile
O] |  +- org.hibernate:hibernate-validator:jar:5.3.6.Final:compile
O] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
O] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
O] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.10:compile
O] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
O] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile
O] |  +- org.springframework:spring-web:jar:4.3.14.RELEASE:compile
O] |  \- org.springframework:spring-webmvc:jar:4.3.14.RELEASE:compile
O] |     \- org.springframework:spring-expression:jar:4.3.14.RELEASE:compile
O] +- com.amazonaws:aws-java-sdk-s3:jar:1.11.288:compile
O] |  +- com.amazonaws:aws-java-sdk-kms:jar:1.11.125:compile
O] |  +- com.amazonaws:aws-java-sdk-core:jar:1.11.125:compile
O] |  |  +- commons-logging:commons-logging:jar:1.1.3:compile
O] |  |  +- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
O] |  |  |  +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
O] |  |  |  \- commons-codec:commons-codec:jar:1.10:compile
O] |  |  +- software.amazon.ion:ion-java:jar:1.0.2:compile
O] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.10:compile
O] |  |  \- joda-time:joda-time:jar:2.9.9:compile
O] |  \- com.amazonaws:jmespath-java:jar:1.11.125:compile
O] +- com.h2database:h2:jar:1.4.196:runtime
O] +- org.projectlombok:lombok:jar:1.16.20:compile
O] \- org.springframework.boot:spring-boot-starter-test:jar:1.5.10.RELEASE:test
O]    +- org.springframework.boot:spring-boot-test:jar:1.5.10.RELEASE:test
O]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.10.RELEASE:test
O]    +- com.jayway.jsonpath:json-path:jar:2.2.0:test
O]    |  \- net.minidev:json-smart:jar:2.2.1:test
O]    |     \- net.minidev:accessors-smart:jar:1.1:test
O]    |        \- org.ow2.asm:asm:jar:5.0.3:test
O]    +- junit:junit:jar:4.12:test
O]    +- org.assertj:assertj-core:jar:2.6.0:test
O]    +- org.mockito:mockito-core:jar:1.10.19:test
O]    |  \- org.objenesis:objenesis:jar:2.1:test
O]    +- org.hamcrest:hamcrest-core:jar:1.3:test
O]    +- org.hamcrest:hamcrest-library:jar:1.3:test
O]    +- org.skyscreamer:jsonassert:jar:1.4.0:test
O]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
O]    +- org.springframework:spring-core:jar:4.3.14.RELEASE:compile
O]    \- org.springframework:spring-test:jar:4.3.14.RELEASE:test
O] ------------------------------------------------------------------------

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>AwsSdkDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AwsSdkDemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.288</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-core</artifactId>
            <version>1.11.288</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

【问题讨论】:

我猜你有一个旧版本的aws-java-sdk-core JAR。运行 mvn dependency:tree 并验证所有 AWS JAR 具有相同的版本。如果您需要更多解释,请使用输出更新您的问题。 谢谢,我更新了 mvn 依赖树。它使用 com.amazonaws:aws-java-sdk-core:jar:1.11.125 这实际上很奇怪:aws-sdk-java-s3 似乎从早期版本的 SDK 中提取依赖项。我快速浏览了POM,这不应该发生:它应该引用具有相同版本的其他模块。 这引出了两个问题:首先,您的项目 POM 中是否有一个 &lt;dependencyManagement&gt; 来设置 SDK 版本?还是你定义属性awsjavasdk.version 是的。有一连串的依赖管理声明以 here 结尾,这将 AWS 版本限制为 1.11.125。我建议不要与此作斗争,而是删除您在 POM 中引用的 AWS JAR 的 &lt;version&gt; 规范。 【参考方案1】:

谢谢大家,问题在于 spring 提取的 aws-core 依赖项版本不一致。手动提供核心版本(1.11.288)后,它解决了问题。如前所述,如果我们在 spring boot 中不显式地将 version 放入 pom 中,它也可以解决这个问题。

【讨论】:

【参考方案2】:

我遇到了类似的问题com.amazonaws:aws-java-sdk-secretsmanager:jar:1.11.430 正在引入com.amazonaws:aws-java-sdk-core:jar:1.11.125 来解决这个问题,具体方法是在 pom 中包含 core-sdk jar:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-core</artifactId>
    <version>1.11.430</version>
</dependency>

我还针对 aws-java-sdk 提出了一个问题:https://github.com/aws/aws-sdk-java/issues/1787

【讨论】:

为我工作,但我用的是最新的【参考方案3】:

虽然这是一个旧帖子,但我正在发布一个解决方案。希望对您有所帮助。

按照以下步骤操作(如果问题得到解决,您可以随时跳过剩余步骤):

    确保sdk-core 版本和客户端版本相同。如果不是,则 sdk-core 的版本必须高于客户端库 为了使sdk-core 和客户端添加相同的版本 ->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.308</version> <-- Add recent version
            <type>pom</type>
            <scope>import</scope>   <-- don't remove
        </dependency>
    </dependencies>
</dependencyManagement>

    运行mvn dependency:tree 并检查不匹配的位置。

    添加此行以捕获引用System.out.println(HandlerContextKey.class.getProtectionDomain().getCodeSource().getLocation().getPath());

    最后清除你的依赖缓存。如果您使用的是任何 IDE,那么也使该缓存无效

添加第 4 步中提到的代码后对我有用。 sdk-core 依赖被其他依赖添加。

【讨论】:

【参考方案4】:

最好不要手动提供 'aws-java-sdk-core' 版本,让 spring 它为您和您的应用程序使用 Spring boot2 自行决定。

【讨论】:

以上是关于使用 aws-java-sdk-s3 时出错的主要内容,如果未能解决你的问题,请参考以下文章

VS2010 安装Opencv2.3时出错

使用 Eclipse 从 OpenCV-3.1 读取时出错

在 pig 中使用 ASSERT 时出错

使用 Swift 3 在 Magical Record 中保存上下文时出错

使用 GridSearchCV 但不使用 GridSearchCV 时出错 - Python 3.6.7

尝试在 Python 3 中使用 C++ 类时出错