连接到Amazon S3时,获取HttpHostConnection时出错

时间:2013-11-05 14:36:13

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

我正在使用java代码连接到amazon s3。每次我尝试连接S3时,我都会收到HttpHostConnection拒绝错误。在S3我只有一个桶作为所有者拥有对我的所有权限。每当我尝试在存储桶中列出文件或尝试在S3上传文件时,我都会收到错误。

代码如下:

    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 

5 个答案:

答案 0 :(得分:9)

经过2个月的努力,我找到了解决办法。在这里,我们需要检查来自我们网络的代理设置。我只是创建了ClientConfig并为此对象设置了代理属性。我使用putObjectRequest方法上传对象。这是一个代码

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

答案 1 :(得分:0)

如果您具有正确的ACCESS_KEY和SECRET_KEY以连接到AWS S3,则可能需要检查凭据。您可以通过IAM管理控制台中的安全凭证获取它。

答案 2 :(得分:0)

为了增加上述答案,我也遇到了这个问题,原因是路由/ DNS问题(通常显示为嵌套的UnknownHostException)

  • 对s3 URI进行nslookup(例如nslookup s3.amazonaws.com
  • 如果您使用的是VPN,请确保它确实正常工作(因为许多人在挂断远程端时会显示“活动”,尤其是在虚拟机上)。不正确/失败的nslookup也应该显示这个

答案 3 :(得分:0)

就我而言,这是一个代理问题。在TS3Connection组件上,在高级设置中,我已使用客户端参数检查了配置客户端:代理主机,代理端口及其工作

答案 4 :(得分:0)

设置AmazonS3对象时,将传入ClientConfiguration对象。

例如:

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)...;

.withProxyHost()值错误。