AWS套接字不是由此工厂创建的

时间:2018-02-21 14:38:24

标签: amazon-web-services spring-boot amazon-s3

我正在尝试将一些对象放入s3,并且它会在不是由这个工厂创建的aws套接字上抛出错误Exception任何人都有这方面的线索吗?

它在这个功能上失败了

public void uploadToS3(Object obj) {
        try {
            ByteArrayInputStream input = new ByteArrayInputStream(obj.toString().getBytes());
            s3client.listBuckets();
            s3client.putObject(bucketName,fileName,input,new ObjectMetadata());
        }
        catch(AmazonServiceException e) {
            System.out.println(e.toString());
        }

    }

这是错误消息

此工厂未在org.apache.http.util.Asserts.check(Asserts.java:34)的org.apache.http.conn.ssl.SSLSocketFactory.isSecure(SSLSocketFactory.java:435)处创建套接字org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:186)在org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)在org.apache.http.impl。 client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)atg.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient。 java:835)atg.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)at com.amazonaws的org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) .http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:749)at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpCli ent.java:505)在com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:317)在com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3595)在com.amazonaws.services.s3 .AmazonS3Client.invoke(AmazonS3Client.java:3548)在com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:697)在com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:703)在com.mobacar.service.DataModelService.uploadToS3(DataModelService.java:33)位于sun.reflect的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)中的com.mobacar.handler.UnconvertedRiDataModelMessageHandler.handle(UnconvertedRiDataModelMessageHandler.java:38)。 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Sour

中的NativeMethodAccessorImpl.invoke(未知来源)

这是配置类:

@Configuration
public class S3Config {

    @Value("${aws.accessKey}")
    private String accessKey;

    @Value("${aws.secretKey}")
    private String secretKey;

    @Value("${aws.region}")
    private String region;

    @Bean
    public BasicAWSCredentials basicAWSCredentials() {
        return new BasicAWSCredentials(accessKey, secretKey);
    }

    @Bean(name="amazonClient")
    public AmazonS3Client amazonS3Client(AWSCredentials awsCredentials) {
        AmazonS3Client amazonS3Client = new AmazonS3Client(awsCredentials);
        amazonS3Client.setRegion(Region.getRegion(Regions.fromName(region)));
        return amazonS3Client;
    }


}

以下是依赖项:

<dependencies>
        <dependency>
            <groupId>com.mobacar.searchmanager</groupId>
            <artifactId>valueObject</artifactId>
             <version>0.0.1-SNAPSHOT</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
             <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Starter Dependecy S3 -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.10.39</version>
        </dependency>
        <!--  -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
                <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-kafka</artifactId>
        </dependency>
    </dependencies>

3 个答案:

答案 0 :(得分:4)

我终于明白了。 Amazon java sdk需要不同版本的htppclient,它不是来自spring管理的httpclient。所以只需要添加这种依赖。

        <dependency>
         <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
         <version>4.5</version>
    </dependency>

答案 1 :(得分:0)

将项目迁移到Java 11 + Spring Boot 2.1.8时遇到相同的问题。 解决方法是什么?将 aws-java-sdk 升级到以下版本以解决此问题。

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

答案 2 :(得分:0)

我在 Java 8 + aws-java-sdk-s3 版本 1.9.32 中遇到了同样的问题

我刚刚将我的 httpclient 版本更新为 4.5.1 并解决了我的问题

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.1</version>
    <type>jar</type>
</dependency>