use of com.amazonaws.services.s3.model.AmazonS3Exception in project aws-doc-sdk-examples by awsdocs.
the class CreateBucket method createBucket.
public static Bucket createBucket(String bucket_name) {
final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
Bucket b = null;
if (s3.doesBucketExistV2(bucket_name)) {
System.out.format("Bucket %s already exists.\n", bucket_name);
b = getBucket(bucket_name);
} else {
try {
b = s3.createBucket(bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
}
return b;
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project aws-doc-sdk-examples by awsdocs.
the class App method main.
public static void main(String[] args) {
if (args.length < 2) {
System.out.format("Usage: <the bucket name> <the AWS Region to use>\n" + "Example: my-test-bucket us-east-2\n");
return;
}
String bucket_name = args[0];
String region = args[1];
s3 = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(region).build();
// List current buckets.
ListMyBuckets();
// Create the bucket.
if (s3.doesBucketExistV2(bucket_name)) {
System.out.format("\nCannot create the bucket. \n" + "A bucket named '%s' already exists.", bucket_name);
return;
} else {
try {
System.out.format("\nCreating a new bucket named '%s'...\n\n", bucket_name);
s3.createBucket(new CreateBucketRequest(bucket_name, region));
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
}
// Confirm that the bucket was created.
ListMyBuckets();
// Delete the bucket.
try {
System.out.format("\nDeleting the bucket named '%s'...\n\n", bucket_name);
s3.deleteBucket(bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
// Confirm that the bucket was deleted.
ListMyBuckets();
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.
the class S3FileSystemTest method matchNonGlobNotFoundWithS3Options.
@Test
public void matchNonGlobNotFoundWithS3Options() {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());
S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/nonexistentfile");
AmazonS3Exception exception = new AmazonS3Exception("mock exception");
exception.setStatusCode(404);
when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
MatchResult result = s3FileSystem.matchNonGlobPath(path);
assertThat(result, MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.
the class S3FileSystemTest method matchNonGlobNotFound.
@Test
public void matchNonGlobNotFound() {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("mys3"));
S3ResourceId path = S3ResourceId.fromUri("mys3://testbucket/testdirectory/nonexistentfile");
AmazonS3Exception exception = new AmazonS3Exception("mock exception");
exception.setStatusCode(404);
when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
MatchResult result = s3FileSystem.matchNonGlobPath(path);
assertThat(result, MatchResultMatcher.create(MatchResult.Status.NOT_FOUND, new FileNotFoundException()));
}
use of com.amazonaws.services.s3.model.AmazonS3Exception in project beam by apache.
the class S3FileSystemTest method matchNonGlobForbidden.
@Test
public void matchNonGlobForbidden() {
S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
AmazonS3Exception exception = new AmazonS3Exception("mock exception");
exception.setStatusCode(403);
S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/keyname");
when(s3FileSystem.getAmazonS3Client().getObjectMetadata(argThat(new GetObjectMetadataRequestMatcher(new GetObjectMetadataRequest(path.getBucket(), path.getKey()))))).thenThrow(exception);
assertThat(s3FileSystem.matchNonGlobPath(path), MatchResultMatcher.create(MatchResult.Status.ERROR, new IOException(exception)));
}
Aggregations