Skip to content

Commit

Permalink
Merge pull request classmethod#49 from glebsts/issue-48-create-bucket…
Browse files Browse the repository at this point in the history
…-with-region

classmethod#48 'CreateBucketTask ignores region'
  • Loading branch information
dai0304 authored Aug 4, 2016
2 parents c844b8f + 7921535 commit 86430ab
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ aws {

These credentials are used to make API accesses by default. The format of the credentials file is described in the [Amazon AWS Docs](http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html#credentials-file-format).

### S3 Create bucket

```
apply plugin: 'jp.classmethod.aws.s3'
task createBucket(type: CreateBucketTask) {
bucketName myBucketName
// one of http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region values, us-east-1 by default
region regionName
// create bucket only if it does not exist, otherwise skip
ifNotExists true
}
```

Look [S3 example 1](samples/01-s3-upload-simple)

### S3 files tasks

Expand Down
3 changes: 3 additions & 0 deletions samples/01-s3-upload-simple/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def myBucketName = 'gradle-aws-plugin-sample'

task createBucket(type: CreateBucketTask) {
bucketName myBucketName

// one of http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region values, us-east-1 by default
region regionName
ifNotExists true
}

Expand Down
53 changes: 47 additions & 6 deletions src/main/java/jp/classmethod/aws/gradle/s3/CreateBucketTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package jp.classmethod.aws.gradle.s3;

import com.amazonaws.services.s3.model.Region;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -26,12 +27,31 @@
import com.amazonaws.services.s3.AmazonS3;

public class CreateBucketTask extends ConventionTask {



private static final String AWS_DEFAULT_REGION_NAME = "us-east-1 (default)";

/**
* Amazon S3 bucket names are globally unique, regardless of the AWS region in which you create the bucket.
* http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html
* See also http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
*/
@Getter
@Setter
public String bucketName;


/**
* Region identifier. Even empty value is correct.
* By default, the bucket is created in the US East (N. Virginia) region.
* See http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for details
* Also https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/model/Region.java
*/
@Getter
@Setter
public String region;

/**
* Create bucket only if it does not exists.
*/
@Getter
@Setter
public boolean ifNotExists;
Expand All @@ -46,19 +66,40 @@ public CreateBucketTask() {
public void createBucket() {
// to enable conventionMappings feature
String bucketName = getBucketName();

final String region = getRegion();

if (bucketName == null)
throw new GradleException("bucketName is not specified");

AmazonS3PluginExtension ext = getProject().getExtensions().getByType(AmazonS3PluginExtension.class);
AmazonS3 s3 = ext.getClient();

final boolean createIfNotExists = isIfNotExists();

if (isIfNotExists() == false || exists(s3) == false) {
if (createIfNotExists && exists(s3)) {
getLogger().info("Bucket already exists and won't be created. Use 'ifNotExists' to override.");
return;
}

String regionName = AWS_DEFAULT_REGION_NAME;
if (region == null) {
s3.createBucket(bucketName);
getLogger().info("S3 Bucket '{}' created", bucketName);
} else {
regionName = getAwsRegionName(region);
s3.createBucket(bucketName, region);
}
getLogger().info("S3 Bucket '{}' created at region '{}'", bucketName, regionName);
}


private String getAwsRegionName(final String region) {
try {
return Region.fromValue(region).toString();
} catch (IllegalArgumentException e) {
throw new GradleException(e.getMessage());
}
}

private boolean exists(AmazonS3 s3) {
// to enable conventionMappings feature
String bucketName = getBucketName();
Expand Down

0 comments on commit 86430ab

Please sign in to comment.