Skip to content

Commit

Permalink
adding async stream example
Browse files Browse the repository at this point in the history
  • Loading branch information
soo-aws committed Aug 8, 2017
1 parent ff3adad commit daa37a9
Showing 1 changed file with 52 additions and 0 deletions.
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);
}
});
}
}

0 comments on commit daa37a9

Please sign in to comment.