use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3BucketDeletion method listAllObjects.
// snippet-start:[s3.java2.s3_bucket_ops.delete_bucket]
public static void listAllObjects(S3Client s3, String bucket) {
try {
// To delete a bucket, all the objects in the bucket must be deleted first
ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).build();
ListObjectsV2Response listObjectsV2Response;
do {
listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request);
for (S3Object s3Object : listObjectsV2Response.contents()) {
s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(s3Object.key()).build());
}
listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucket).continuationToken(listObjectsV2Response.nextContinuationToken()).build();
} while (listObjectsV2Response.isTruncated());
// snippet-end:[s3.java2.s3_bucket_ops.delete_bucket]
DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build();
s3.deleteBucket(deleteBucketRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3Cors method getBucketCorsInformation.
public static void getBucketCorsInformation(S3Client s3, String bucketName, String accountId) {
try {
GetBucketCorsRequest bucketCorsRequest = GetBucketCorsRequest.builder().bucket(bucketName).expectedBucketOwner(accountId).build();
GetBucketCorsResponse corsResponse = s3.getBucketCors(bucketCorsRequest);
List<CORSRule> corsRules = corsResponse.corsRules();
for (CORSRule rule : corsRules) {
System.out.println("allowOrigins: " + rule.allowedOrigins());
System.out.println("AllowedMethod: " + rule.allowedMethods());
}
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3Cors method setCorsInformation.
public static void setCorsInformation(S3Client s3, String bucketName, String accountId) {
List<String> allowMethods = new ArrayList();
allowMethods.add("PUT");
allowMethods.add("POST");
allowMethods.add("DELETE");
List<String> allowOrigins = new ArrayList();
allowOrigins.add("http://example.com");
try {
// Define CORS rules.
CORSRule corsRule = CORSRule.builder().allowedMethods(allowMethods).allowedOrigins(allowOrigins).build();
List<CORSRule> corsRules = new ArrayList<>();
corsRules.add(corsRule);
CORSConfiguration configuration = CORSConfiguration.builder().corsRules(corsRules).build();
PutBucketCorsRequest putBucketCorsRequest = PutBucketCorsRequest.builder().bucket(bucketName).corsConfiguration(configuration).expectedBucketOwner(accountId).build();
s3.putBucketCors(putBucketCorsRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3Cors method deleteBucketCorsInformation.
// snippet-start:[s3.java2.cors.main]
public static void deleteBucketCorsInformation(S3Client s3, String bucketName, String accountId) {
try {
DeleteBucketCorsRequest bucketCorsRequest = DeleteBucketCorsRequest.builder().bucket(bucketName).expectedBucketOwner(accountId).build();
s3.deleteBucketCors(bucketCorsRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class S3ObjectOperations method createBucket.
// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName, Region region) {
S3Waiter s3Waiter = s3Client.waiter();
try {
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(CreateBucketConfiguration.builder().locationConstraint(region.id()).build()).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations