gpt4 book ai didi

java - 连接到 Amazon S3 时出现 HttpHostConnection 拒绝错误

转载 作者:行者123 更新时间:2023-11-29 10:02:31 25 4
gpt4 key购买 nike

我正在使用 java 代码连接到 amazon s3。每次尝试连接到 S3 时,我都会收到 HttpHostConnection refused 错误。在 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

最佳答案

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

关于java - 连接到 Amazon S3 时出现 HttpHostConnection 拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19791466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com