10
votes

I am trying to put some objects into s3 and it throws error on aws socket not created by this factory Exception anyone having any clue of this?

Its failing on this function

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());
        }

    }

This is the error message

Socket not created by this factory at org.apache.http.util.Asserts.check(Asserts.java:34) at org.apache.http.conn.ssl.SSLSocketFactory.isSecure(SSLSocketFactory.java:435) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:186) at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:749) at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:505) at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:317) at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3595) at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3548) at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:697) at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:703) at com.mobacar.service.DataModelService.uploadToS3(DataModelService.java:33) at com.mobacar.handler.UnconvertedRiDataModelMessageHandler.handle(UnconvertedRiDataModelMessageHandler.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Sour

and this is the config class:

@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;
    }


}

Here are the dependencies:

<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
Please share the existing code and the exact error messageNico Haase
See error msg in the answer please and the config class?Faraz Tahir
No. Add every relevant information to your questionNico Haase
But this is a comment where stuff is not readable and not the questionNico Haase
I hope its readable now?Faraz Tahir

3 Answers

13
votes

I finally figured it out. Amazon java sdk needs a different version of htppclient which is not coming from spring managed httpclient. So just need to add this dependency.

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

I had the same issue when I migrating our project to Java 11 + Spring Boot 2.1.8. What is the fix? Upgraded aws-java-sdk to the following version to fix this issue.

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

I Faced the same issue with Java 8 + aws-java-sdk-s3 with version 1.9.32

I just updated my httpclient version to 4.5.1 and that resolved my issue

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