@Override protected String parse(String input) throws IllegalArgumentException { AmazonS3 s3 = PersistS3.getClient(); if (!s3.doesBucketExist(input)) throw new IllegalArgumentException("S3 Bucket " + input + " not found!"); return input; }
private static void createResources() { ClasspathPropertiesFileCredentialsProvider provider = new ClasspathPropertiesFileCredentialsProvider(); AmazonSQS sqs = new AmazonSQSClient(provider); sqs.setEndpoint(SQS_ENDPOINT); sqs.createQueue(new CreateQueueRequest().withQueueName(SQS_QUEUE)); AmazonS3 s3 = new AmazonS3Client(provider); if (!s3.doesBucketExist(BUCKET_NAME)) { s3.createBucket(new CreateBucketRequest(BUCKET_NAME)); } AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient(provider); dynamo.setEndpoint(DYNAMO_ENDPOINT); if (!doesTableExist(dynamo, DYNAMO_TABLE_NAME)) { dynamo.createTable( new CreateTableRequest() .withTableName(DYNAMO_TABLE_NAME) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(50l) .withWriteCapacityUnits(50l)) .withKeySchema( new KeySchema() .withHashKeyElement( new KeySchemaElement() .withAttributeName("id") .withAttributeType(ScalarAttributeType.S)))); waitForTableToBecomeAvailable(dynamo, DYNAMO_TABLE_NAME); } }
/** * AwsBucket can throw a proper exception. * * @throws IOException If succeeds */ @Test(expected = IOException.class) public void existsThrowsIOException() throws IOException { final Region region = Mockito.mock(Region.class); final AmazonS3 aws = Mockito.mock(AmazonS3.class); Mockito.when(region.aws()).thenReturn(aws); final String name = "throwing.bucket.com"; Mockito.when(aws.doesBucketExist(name)).thenThrow(new AmazonServiceException("Test exception")); final Bucket bucket = new AwsBucket(region, name); bucket.exists(); }
/** * AwsBucket can correctly check the existence of the non-existing bucket. * * @throws IOException If fails */ @Test public void existsNonExistingBucket() throws IOException { final Region region = Mockito.mock(Region.class); final AmazonS3 aws = Mockito.mock(AmazonS3.class); Mockito.when(region.aws()).thenReturn(aws); final String name = "non.existing.bucket.com"; Mockito.when(aws.doesBucketExist(name)).thenReturn(false); final Bucket bucket = new AwsBucket(region, name); Assert.assertFalse(bucket.exists()); }
@Override public void createContainer(String container, Config config) { super.createContainer(container, config); try { if (!client.doesBucketExist(container)) { client.createBucket(container); } } catch (Exception e) { throw new StorageException(e); } }
@Override public void deleteContainer(String container, Config config) { super.deleteContainer(container, config); try { if (client.doesBucketExist(container)) { client.deleteBucket(container); } } catch (AmazonS3Exception awse) { if (awse.getStatusCode() != HttpStatus.SC_NOT_FOUND) { throw new StorageException(awse); } } catch (Exception e) { throw new StorageException(e); } }
@Inject public AmazonS3Storage(Configuration configuration) { bucketName = configuration.getString("storage.s3.bucket"); String accessKey = configuration.getString("storage.s3.accesskey"); String secretKey = configuration.getString("storage.s3.secretkey"); credentials = new BasicAWSCredentials(accessKey, secretKey); AmazonS3 amazonS3 = new AmazonS3Client(credentials); try { if (!(amazonS3.doesBucketExist(bucketName))) { amazonS3.createBucket(new CreateBucketRequest(bucketName)); } String bucketLocation = amazonS3.getBucketLocation(new GetBucketLocationRequest(bucketName)); Logger.info("Amazon S3 bucket created at " + bucketLocation); } catch (AmazonServiceException ase) { Logger.error( "Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response " + "for some reason." + " Error Message: " + ase.getMessage() + " HTTP Status Code: " + ase.getStatusCode() + " AWS Error Code: " + ase.getErrorCode() + " Error Type: " + ase.getErrorType() + " Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { Logger.error( "Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network." + " Error Message: " + ace.getMessage()); } }