AWS Code Sample
Catalog

S3BucketOps.java

S3BucketOps.java demonstrates how to create, list and delete S3 buckets.

/* * Copyright 2011-2018 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.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration; import software.amazon.awssdk.services.s3.model.CreateBucketRequest; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; import software.amazon.awssdk.services.s3.model.ListBucketsRequest; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; public class S3BucketOps { public static void main(String[] args) { Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); String bucket = "bucket" + System.currentTimeMillis(); System.out.println(bucket); // Create bucket CreateBucketRequest createBucketRequest = CreateBucketRequest .builder() .bucket(bucket) .createBucketConfiguration(CreateBucketConfiguration.builder() .locationConstraint(region.id()) .build()) .build(); s3.createBucket(createBucketRequest); // List buckets ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build(); ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest); listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name())); // Delete empty bucket DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); s3.deleteBucket(deleteBucketRequest); } }

Sample Details

Service: s3

Author: soo-aws

Type: full-example

On this page: