forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
javav2/example_code/s3/src/main/java/com/example/s3/S3AsyncStreamOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2011-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.example.s3; | ||
|
||
import software.amazon.awssdk.async.AsyncResponseHandler; | ||
import software.amazon.awssdk.services.s3.S3AsyncClient; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.utils.FunctionalUtils; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class S3AsyncStreamOps { | ||
|
||
private static final String BUCKET = "sample-bucket"; | ||
private static final String KEY = "testfile.out"; | ||
|
||
public static void main(String[] args) { | ||
S3AsyncClient client = S3AsyncClient.create(); | ||
final CompletableFuture<Void> futureGet = client.getObject( | ||
GetObjectRequest.builder() | ||
.bucket(BUCKET) | ||
.key(KEY) | ||
.build(), | ||
AsyncResponseHandler.toFile(Paths.get("myfile.out"))); | ||
futureGet.whenComplete((resp, err) -> { | ||
try { | ||
if (resp != null) { | ||
System.out.println(resp); | ||
} else { | ||
// Handle error | ||
err.printStackTrace(); | ||
} | ||
} finally { | ||
// Lets the application shut down. Only close the client when you are completely done with it | ||
FunctionalUtils.invokeSafely(client::close); | ||
} | ||
}); | ||
} | ||
} |