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

时间:2018-03-03 14:20:24

标签: amazon-web-services amazon-s3 aws-sdk aws-java-sdk

我正在尝试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>

4 个答案:

答案 0 :(得分:6)

谢谢大家,问题在于没有一致的版本的aws-core依赖性被春天拉动。手动提供核心版本(1.11.288)后,它解决了问题。如前所述,如果我们不在Spring引导中明确地将版本放在pom中,它也可以解决这个问题。

答案 1 :(得分: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

答案 2 :(得分:0)

尽管这是旧文章,但我正在发布解决方案。希望对您有所帮助。

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

  
      
  1. 确保sdk-core版本和客户端版本相同。如果不是,则sdk-core的版本必须高于客户端库
  2.   
  3. 为了使sdk-core和客户端具有相同的版本,请添加->
  4.   

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

  
      
  1. 运行mvn dependency:tree并检查不匹配的地方。

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

  4.   
  5. 最后,清除依赖项缓存。如果您使用的是任何IDE,也请使该缓存无效

  6.   

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

答案 3 :(得分:0)

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