AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above. demonstrates how to use the S3 asynchronous client.

/* * Copyright 2011-2019, 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: * * * * 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; import; import; import; import; import java.nio.file.Paths; import java.util.concurrent.CompletableFuture; public class S3AsyncOps { private static final String BUCKET = "sample-bucket"; private static final String KEY = ""; public static void main(String[] args) { S3AsyncClient client = S3AsyncClient.create(); CompletableFuture<PutObjectResponse> future = client.putObject( PutObjectRequest.builder() .bucket(BUCKET) .key(KEY) .build(), AsyncRequestBody.fromFile(Paths.get("")) ); future.whenComplete((resp, err) -> { try { if (resp != null) { System.out.println("my response: " + resp); } else { // Handle error err.printStackTrace(); } } finally { // Lets the application shut down. Only close the client when you are completely done with it. client.close(); } }); future.join(); } }

Sample Details

Service: s3

Author: soo-aws

Type: full-example

On this page: