Skip to content

Commit

Permalink
Add support for bucket aliases in env vars or Java system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesde committed Oct 3, 2018
1 parent 3c2d71c commit 4dd16de
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions MoquiConf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
<default-property name="AWS_ACCESS_KEY_ID" value=""/>
<default-property name="AWS_SECRET_ACCESS_KEY" value=""/>

<!-- these must be used in pairs, if alias used in a aws3://{bucket}/... then is replaced with name -->
<!-- only 3 sets here by default, code looks for 1-9 -->
<default-property name="aws_s3_bucket_alias1" value=""/>
<default-property name="aws_s3_bucket_name1" value=""/>
<default-property name="aws_s3_bucket_alias2" value=""/>
<default-property name="aws_s3_bucket_name2" value=""/>
<default-property name="aws_s3_bucket_alias3" value=""/>
<default-property name="aws_s3_bucket_name3" value=""/>

<tools>
<tool-factory class="org.moqui.aws.S3ClientToolFactory" init-priority="40" disabled="false"/>
</tools>
Expand Down
29 changes: 27 additions & 2 deletions src/main/groovy/org/moqui/aws/S3ResourceReference.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package org.moqui.aws

import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.AmazonS3Exception
import com.amazonaws.services.s3.model.CopyObjectRequest
import com.amazonaws.services.s3.model.GetObjectMetadataRequest
import com.amazonaws.services.s3.model.GetObjectRequest
import com.amazonaws.services.s3.model.ListObjectsV2Request
Expand Down Expand Up @@ -64,6 +63,26 @@ class S3ResourceReference extends BaseResourceReference {
public final static String locationPrefix = "aws3://"
public final static boolean autoCreateBucket = true

// don't static init this, just in case inits before ExecutionContextFactoryImpl inits and sets default properties
private static Map<String, String> bucketAliasMapInternal = null
static Map<String, String> getBucketAliasMap() {
if (bucketAliasMapInternal != null) return bucketAliasMapInternal
Map<String, String> tempAliasMap = new HashMap<>()
for (int i = 1; i < 9; i++) {
String alias = System.getProperty("aws_s3_bucket_alias" + i)
String name = System.getProperty("aws_s3_bucket_name" + i)
if (alias && name) {
tempAliasMap.put(alias, name)
} else {
alias = System.getenv("aws_s3_bucket_alias" + i)
name = System.getenv("aws_s3_bucket_name" + i)
if (alias && name) tempAliasMap.put(alias, name)
}
}
bucketAliasMapInternal = tempAliasMap
return bucketAliasMapInternal
}

String location
Boolean knownDirectory = (Boolean) null

Expand Down Expand Up @@ -95,6 +114,12 @@ class S3ResourceReference extends BaseResourceReference {
int slashIdx = fullPath.indexOf("/")
String bName = slashIdx == -1 ? fullPath : fullPath.substring(0, slashIdx)
if (!bName) throw new BaseArtifactException("No bucket name (first path segment) in location ${location}")

// see if bucket name is an alias
Map<String, String> aliasMap = getBucketAliasMap()
String aliasName = aliasMap.get(bName)
if (aliasName != null && !aliasName.isEmpty()) bName = aliasName

return bName
}
static String getPath(String location) {
Expand Down Expand Up @@ -287,10 +312,10 @@ class S3ResourceReference extends BaseResourceReference {
}
@Override void putStream(InputStream stream) {
if (stream == null) return
AmazonS3 s3Client = getS3Client()
String bucketName = getBucketName(location)
String path = getPath(location)

AmazonS3 s3Client = getS3Client()
if (autoCreateBucket && !s3Client.doesBucketExistV2(bucketName)) s3Client.createBucket(bucketName)

// NOTE: can specify more options using ObjectMetadata object as 4th parameter
Expand Down

0 comments on commit 4dd16de

Please sign in to comment.