我已经编写了一个代码来列出amazon 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)
使用main方法列出文件的代码:
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());
});
}
}
如果我通过执行main方法运行代码,此代码将正常工作,但如果我通过调用Spring restful webservice从tomcat服务器运行代码,则会发生异常。
目前我使用的是最新的Tomcat服务器8.5.28,有什么方法可以解决这个问题吗?
答案 0 :(得分:1)
EndpointConfiguration
适用于亚马逊的端点,而非您的端点。来自documentation:
每个AWS客户端都可以配置为使用a中的特定端点 通过调用setEndpoint方法来区域。
例如,配置Amazon EC2客户端以使用EU (爱尔兰)地区,请使用以下代码。
AmazonEC2 ec2 = new AmazonEC2(myCredentials); ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com");