· 8 years ago · Dec 11, 2017, 04:00 AM
1S3AsyncClient client = S3AsyncClient.create();
2 final CompletableFuture<GetObjectResponse> futureGet =
3 client.getObject(
4 GetObjectRequest.builder()
5 .bucket(awsS3Config.getBucketName())
6 .key(finalKeyName)
7 .build(),
8
9 AsyncResponseHandler.toFile(Paths.get(s3TmpFile.getAbsolutePath())));
10 futureGet.whenComplete((resp, err) -> {
11 try {
12 if (resp != null) {
13 System.out.println(resp);
14 } else {
15 // Handle error
16 err.printStackTrace();
17 }
18 } finally {
19 // Lets the application shut down. Only close the client when you are completely done with it
20 FunctionalUtils.invokeSafely(client::close);
21 }
22 });
23
24if (ENVIRONMENT_LOCAL)
25 {
26 ClientConfiguration clientConfig = new ClientConfiguration();
27 clientConfig.setProtocol(Protocol.HTTP);
28 clientConfig.setProxyHost(8888);
29
30 //Attempt to connect using Access Key and SecretKey
31 BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(awsS3Config.getIamKey(), awsS3Config.getIamSecretKey());
32
33 clientConfig.setProxyHost("myproxyhost.bla");
34 clientConfig.setProxyPort(8888);
35 AWSStaticCredentialsProvider
36 awsStaticCredentialsProvider =
37 new AWSStaticCredentialsProvider(basicAWSCredentials);
38
39 this.amazonS3 = AmazonS3ClientBuilder.standard()
40 .withRegion(awsS3Config.getRegion())
41 .withClientConfiguration(clientConfig)
42 .withCredentials(awsStaticCredentialsProvider)
43 .build();
44 }
45 else //not local env, use EC2 instance authentication
46 {
47 this.amazonS3 = AmazonS3ClientBuilder.standard()
48 .withRegion(awsS3Config.getRegion())
49 .withCredentials(new
50 InstanceProfileCredentialsProvider(false))
51 .build();
52 }