Skip to content

Instantly share code, notes, and snippets.

@ldaley
Created December 8, 2014 21:39
Show Gist options
  • Save ldaley/3aef34850b5447fd54c3 to your computer and use it in GitHub Desktop.
Save ldaley/3aef34850b5447fd54c3 to your computer and use it in GitHub Desktop.
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