无法从EC2连接到S3

时间:2015-04-14 14:39:32

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

我正在尝试使用AmazonS3Client从EC2实例连接到S3,以获取S3存储桶中存在的对象列表。虽然从本地计算机运行此代码时可以连接到S3,但我很难在EC2上运行相同的代码。

我错过了EC2实例上的任何设置或配置吗?

代码

AWSCredentials credentials = new BasicAWSCredentials("XXXX", "YYYY");

AmazonS3Client conn = new AmazonS3Client(credentials);

String bucketName = "s3-xyz";

String prefix = "123";

ObjectListing objects = conn.listObjects(bucketName, prefix);

List<S3ObjectSummary> objectSummary = objects.getObjectSummaries();

for(S3ObjectSummary os : objectSummary)
{
        System.out.println(os.getKey());
}

错误

ERROR com.amazonaws.http.AmazonHttpClient  - Unable to execute HTTP request: Connect to s3-xyz.amazonaws.com:443 timed out
    org.apache.http.conn.ConnectTimeoutException: Connect to s3-xyz.s3.amazonaws.com:443 timed out
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:551)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
    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:318)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:202)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3037)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3008)
    at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:531)
    at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:515)  

1 个答案:

答案 0 :(得分:1)

ClientConfiguration cc = new ClientConfiguration(); 
    cc.setProxyHost("10.66.80.122"); 
    cc.setProxyPort(8080);      
    propertiesCredentials = new BasicAWSCredentials(aws_access_key_id, aws_secret_access_key);
    s3 = new AmazonS3Client(propertiesCredentials,cc);

查找proxy_host&amp;端口转到LAN设置。Click to see image.