use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class S3BucketDeletion method main.
public static void main(String[] args) throws Exception {
final String USAGE = "\n" + "Usage:\n" + " <bucket>\n\n" + "Where:\n" + " bucket - the bucket to delete (for example, bucket1). \n\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String bucket = args[0];
Region region = Region.US_WEST_2;
S3Client s3 = S3Client.builder().region(region).build();
listAllObjects(s3, bucket);
s3.close();
}
use of software.amazon.awssdk.services.s3.S3Client 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.S3Client 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.S3Client 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.S3Client in project aws-doc-sdk-examples by awsdocs.
the class S3Cors method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " <bucketName> <accountId> \n\n" + "Where:\n" + " bucketName - the Amazon S3 bucket to upload an object into.\n" + " accountId - the id of the account that owns the Amazon S3 bucket.\n";
;
String bucketName = args[0];
String accountId = args[1];
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().region(region).build();
setCorsInformation(s3, bucketName, accountId);
getBucketCorsInformation(s3, bucketName, accountId);
deleteBucketCorsInformation(s3, bucketName, accountId);
s3.close();
}
Aggregations