AWS Code Sample
Catalog demonstrates how to make basic requests against Amazon S3 resources.

/** * Copyright 2018-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located 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. */ import; import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import; import; import; import; import; public class MakingRequests { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Get a list of objects in the bucket, two at a time, and // print the name and size of each object. ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(bucketName).withMaxKeys(2); ObjectListing objects = s3Client.listObjects(listRequest); while(true) { List<S3ObjectSummary> summaries = objects.getObjectSummaries(); for(S3ObjectSummary summary : summaries) { System.out.printf("Object \"%s\" retrieved with size %d\n", summary.getKey(), summary.getSize()); } if(objects.isTruncated()) { objects = s3Client.listNextBatchOfObjects(objects); } else { break; } } } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

Sample Details

Service: s3

Last tested: 2019-01-28

Author: AWS

Type: full-example

On this page: