Created
December 8, 2014 21:39
-
-
Save ldaley/3aef34850b5447fd54c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.Action | |
import org.gradle.api.tasks.Input | |
import org.gradle.api.tasks.Optional | |
import org.gradle.api.tasks.SourceTask | |
import org.gradle.api.tasks.TaskAction | |
import org.gradle.listener.ActionBroadcast | |
import org.jets3t.service.S3Service | |
import org.jets3t.service.impl.rest.httpclient.RestS3Service | |
import org.jets3t.service.model.S3Object | |
import org.jets3t.service.model.StorageObject | |
import org.jets3t.service.security.AWSCredentials | |
import org.jets3t.service.security.ProviderCredentials | |
class S3PutTask extends SourceTask { | |
@Input String accessKey | |
@Input String secretKey | |
@Input String bucketName | |
@Input @Optional String friendlyName | |
String buildTimestamp | |
protected ActionBroadcast<S3Object> eachObjects = new ActionBroadcast<S3Object>() | |
void eachObject(Action<S3Object> action) { | |
eachObjects.add(action) | |
} | |
@TaskAction | |
void put() { | |
def service = createService() | |
source.each { File file -> | |
def s3Object = new S3Object(file) | |
eachObjects.execute(s3Object) | |
logger.lifecycle "uploading '$file.name' to '$s3Object.key'" | |
service.putObject(bucketName, s3Object) | |
// S3 incorrectly treats “+” incorrectly as a “ ” | |
// https://forums.aws.amazon.com/thread.jspa?threadID=55746 | |
// We compensate by also providing files with spaces (for unencoded “+”) and files with “+” (for encoded “+”) | |
def key = s3Object.key | |
if (s3Object.key.contains("+")) { | |
def copyDestination = key.replace("+", " ") | |
logger.lifecycle "making copy of '$key' to '$copyDestination'" | |
service.copyObject(bucketName, key, bucketName, new StorageObject(copyDestination), false) | |
} | |
} | |
} | |
ProviderCredentials createCredentials() { | |
new AWSCredentials(accessKey, secretKey, friendlyName) | |
} | |
S3Service createService() { | |
new RestS3Service(createCredentials()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment