无法通过 Tomcat 服务器连接到 Amazon S3
Not able to connect to Amazon S3 via Tomcat server
我已经编写了一个代码来列出 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.access0(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 来自 tomcat 服务器的代码restful webservice 会出现异常
目前我使用的是最新的 Tomcat 服务器 8.5.28,有什么办法可以解决这个问题吗?
EndpointConfiguration
适用于 Amazon 的 端点,而不是您的端点。来自 documentation:
Each AWS client can be configured to use a specific endpoint within a
region by calling the setEndpoint method.
For example, to configure the Amazon EC2 client to use the EU
(Ireland) Region, use the following code.
AmazonEC2 ec2 = new AmazonEC2(myCredentials);
ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com");
我已经编写了一个代码来列出 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.access0(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 来自 tomcat 服务器的代码restful webservice 会出现异常
目前我使用的是最新的 Tomcat 服务器 8.5.28,有什么办法可以解决这个问题吗?
EndpointConfiguration
适用于 Amazon 的 端点,而不是您的端点。来自 documentation:
Each AWS client can be configured to use a specific endpoint within a region by calling the setEndpoint method.
For example, to configure the Amazon EC2 client to use the EU (Ireland) Region, use the following code.
AmazonEC2 ec2 = new AmazonEC2(myCredentials); ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com");