简体   繁体   English

无法通过Tomcat服务器连接到Amazon S3

[英]Not able to connect to Amazon S3 via Tomcat server

I have written a code to list files in amazon s3 bucket , but it gives me this exception : 我已经写了一个代码来列出亚马逊s3存储桶中的文件,但是它给了我这个例外:

com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to 11.36.134.067:8080 [/11.36.134.067] failed: Connection refused: connect
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleRetryableException(AmazonHttpClient.java:1114)
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1064)
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
    com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4330)
    com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4277)
    com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4271)
    com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:835)
    com.focus.re.calibr.service.FileUploadService.list(FileUploadService.java:114)
    com.focus.re.calibr.controller.FileUploadController.list(FileUploadController.java:33)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)

Code to list files with main method: 使用主要方法列出文件的代码:

public class App {

    String bucketName = "buketname";
    String accesskey = "accesskey";

    public static void main(String args[]) throws Exception {
        new App();

    }


    App() throws Exception {

        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accesskey,
                "secretekey");

        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);

        AwsClientBuilder.EndpointConfiguration endpointConfiguration =
                new AwsClientBuilder.EndpointConfiguration("11.36.134.067:8080", "");

        ClientConfiguration clientCfg = new ClientConfiguration();
        SSLContextBuilder builder = new SSLContextBuilder();

        builder.loadTrustMaterial(null, new

                TrustStrategy() {
                    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        return true;
                    }
                });

        SSLConnectionSocketFactory sslConnectionSocketFactory =
                new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
        clientCfg.getApacheHttpClientConfig().

                withSslSocketFactory(sslConnectionSocketFactory);

        AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard()
                .withCredentials(awsCredentialsProvider)
                .withClientConfiguration(clientCfg)
                .withEndpointConfiguration(endpointConfiguration)
                .build();

        listAllFiles(amazonS3);


    } 

public void listAllFiles(AmazonS3 amazonS3) {

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName);

        ObjectListing objectListing = amazonS3. listObjects (listObjectsRequest);

        objectListing.getObjectSummaries().forEach(
                s3ObjectSummary -> {
                    System.out.println(s3ObjectSummary.getKey() + "  " + s3ObjectSummary.getSize());

                });
    }

}

This code will work fine if i run the code by executing the main method , but if i run the code from tomcat server by calling a Spring restful webservice then the exception will occur. 如果我通过执行main方法运行代码,则此代码将正常工作,但如果我通过调用Spring Restful Web服务从tomcat服务器运行代码,则将发生异常。

Currently i am using latest Tomcat server 8.5.28 , is there any way to fix this issue ? 当前我正在使用最新的Tomcat服务器8.5.28,有什么办法可以解决此问题?

EndpointConfiguration is for Amazon's endpoint, not yours. EndpointConfiguration适用于Amazon的端点,而不是您端点。 From the documentation: 文档中:

Each AWS client can be configured to use a specific endpoint within a region by calling the setEndpoint method. 通过调用setEndpoint方法,可以将每个AWS客户端配置为使用区域内的特定终结点。

For example, to configure the Amazon EC2 client to use the EU (Ireland) Region, use the following code. 例如,要将Amazon EC2客户端配置为使用EU(爱尔兰)地区,请使用以下代码。

 AmazonEC2 ec2 = new AmazonEC2(myCredentials); ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM