user2956567
user2956567

Reputation: 111

Getting HttpHostConnection refused error while connecting to Amazon S3

I am using java code to connect to amazon s3. I am getting HttpHostConnection refused error every time I try to connect to S3. On S3 I have only one bucket with all permissions to me as a owner. Whenever I try to list files in bucket or try to upload file on S3 I am getting error.

Code is as follows:

    TransferManager tx = new TransferManager(credentials);  
    Upload upload = tx.upload("bucket.name", doc.getFileName(),                 new File("C:\\testing2.pdf"));   
    while (upload.isDone() == false) {     
       System.out.println(upload.getProgress().getPercentTransferred() + "%"); 
    } 




INFO [s3-transfer-manager-worker-1] (AmazonHttpClient.java434) - Unable to execute HTTP request: Connection to https://bucket.name.s3.amazonaws.com refused
   org.apache.http.conn.HttpHostConnectException: Connection to https://bucket.name.s3.amazonaws.com refused
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:641)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:383)
    at com.amazonaws.http.AmazonHttpClient.execute0(AmazonHttpClient.java:266)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:236)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3104)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1233)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.uploadInOneChunk(UploadCallable.java:108)
    at com.amazonaws.services.s3.transfer.internal.UploadCallable.call(UploadCallable.java:100)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.upload(UploadMonitor.java:192)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:150)
    at com.amazonaws.services.s3.transfer.internal.UploadMonitor.call(UploadMonitor.java:50)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:383)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:245)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:232)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:539)
    at com.ibm.jsse2.tc.connect(tc.java:397)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:549)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    ... 21 more 

Upvotes: 1

Views: 14455

Answers (5)

Gene
Gene

Reputation: 11267

When you setup your AmazonS3 objects, you pass in a ClientConfiguration object.

For example:

ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.withProtocol(Protocol.HTTPS);
clientConfig.withProxyHost(ip_address);
clientConfig.withProxyPort(port number);
clientConfig.withProxyUsername(user id on network);
clientConfig.withProxyPassword(password on network);

AmazonS3 s3Client= AmazonS3ClientBuilder.standard().withClientConfiguration(clientConfig)...;

You have the .withProxyHost() value wrong.

Upvotes: 0

Georgiana
Georgiana

Reputation: 1

In my case, it was a proxy problem. On the TS3Connection component, in Advanced settings i have checked config client with the client parameters: Proxy Host,Proxy Port and it worked

Upvotes: 0

David Beveridge
David Beveridge

Reputation: 560

To augment the above answer, I've encountered this also due to routing/DNS issues (usually showing as a nested UnknownHostException)

  • do an nslookup of the s3 URI (e.g., nslookup s3.amazonaws.com)
  • if you're on a VPN, make sure it's actually working (since many can show "active" while being hung up on the remote end, especially on VMs). an incorrect/failed nslookup should show this as well

Upvotes: 0

user2956567
user2956567

Reputation: 111

After 2months struggle I found solution. Here we need to check proxy setttings from our network. I simply created ClientConfig and set proxy properties to this object. I used putObjectRequest method to upload object. Here is a code

ClientConfiguration clientConfig = new ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTPS);
clientConfig.setProxyHost(ip_address);
clientConfig.setProxyPort(port number);
clientConfig.setProxyUsername(user id on network);
clientConfig.setProxyPassword(password on network);

AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials("access key", "secret key"),
clientConfig);
file = new File(fileName);
PutObjectRequest putObject = new PutObjectRequest(bucketName, fileName, file);
ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentType("application/pdf"); //binary data
putObject.setMetadata(metaData);
s3.putObject(putObject);

Upvotes: 10

ippomakunochi
ippomakunochi

Reputation: 93

You may need to check your credentials if you have the correct ACCESS_KEY and SECRET_KEY to connect to AWS S3. You can get it via the Security Credentials in the IAM Management Console.

Upvotes: 1

Related Questions