A Gradle plugin that uploads files to Amazon S3 and downloads files from Amazon S3.
This project is a fork of mygrocerydeals/gradle-s3-plugin. The original project is no longer maintained.
This fork works with Gradle 9 and later, and it is written in Java.
Apply the plugin in your build script.
Declare the plugin in the catalog:
[versions]
s3-plugin = "2.0.0"
[plugins]
infosmid-s3 = { id = "org.infosmid.plugins.s3", version.ref = "s3-plugin" }Then use the generated libs accessor:
plugins {
alias(libs.plugins.infosmid.s3)
}See the Gradle Plugin Portal page for other versions.
All settings are in the s3 extension.
Each task can override them.
Set region to the AWS region of your bucket.
Use this setting when the authentication profile has no region, or to replace the region of the profile.
s3 {
region = 'us-east-1'
}By default, the plugin uses the
AWS default credentials provider chain.
One way to supply credentials is to set the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
See the AWS documentation for other methods.
You can also set a profile, or an access key and a secret, in the s3 extension.
An access key and secret have priority over a profile.
s3 {
profile = 'my-profile'
awsAccessKeyId = '12345678'
awsSecretAccessKey = 'my-secret'
}|
Warning
|
Do not commit secrets to version control. Read them from a property or an environment variable instead. |
You can define jobs in a configuration block, or you can create tasks of a specific type.
A job in s3Uploads or s3Downloads creates a task.
The task name is the job name plus a suffix:
-
The job
jobNameins3Uploadscreates the taskjobNameUploadTask. -
The job
dlJobins3Downloadscreates the taskdlJobDownloadTask.
s3Uploads {
jobName {
key = 'target-filename'
file = 'source-filename'
}
}
s3Downloads {
dlJob {
keyPrefix = 'folder'
destDir = 'targetDir'
}
}import org.infosmid.gradle.s3.S3Upload
import org.infosmid.gradle.s3.S3Download
task myDownload(type: S3Download) {
keyPrefix = 'folder'
destDir = 'targetDir'
}
task myUpload(type: S3Upload) {
key = 'target-filename'
file = 'source-filename'
compareContent = true
}
task myUploadFiles(type: S3Upload) {
keyPrefix = 'target-folder'
files = fileTree('my-folder').matching {
include('**/*.zip')
}
compareContent = false
}An upload task has three modes:
-
Single file upload
-
Directory upload, including all subdirectories
-
FileCollectionupload
| Property | Default | Description |
|---|---|---|
|
|
S3 bucket to use. |
|
|
AWS access key. |
|
|
AWS secret access key. |
|
|
If |
|
|
If |
|
|
If |
|
|
Number of files to upload in one batch. A larger batch can make the upload faster. |
| Property | Description |
|---|---|
|
Key of the S3 object to create. |
|
Path of the file to upload. |
By default, the task does not replace an S3 object that already exists.
Set overwrite to true to replace it.
| Property | Description |
|---|---|
|
S3 prefix under which the task creates the uploaded objects. |
|
Local directory to upload. |
A directory upload always replaces existing objects under the prefix.
A download task has two modes:
-
Single file download
-
Recursive download
| Property | Default | Description |
|---|---|---|
|
|
S3 bucket to use. |
|
|
AWS access key. |
|
|
AWS secret access key. |
| Property | Description |
|---|---|
|
Key of the S3 object to download. |
|
Local path where the task saves the file. |
| Property | Description |
|---|---|
|
S3 prefix of the objects to download. |
|
Local directory where the task saves the objects. |
A recursive download keeps the full keyPrefix in the local path.
For example, a bucket has these object keys:
top/foo/bar top/README
This download:
s3Downloads {
downloadRecursive {
keyPrefix = 'top/foo/'
destDir = 'local-dir'
}
}creates this local tree:
local-dir/
└── top
└── foo
└── bar
The task downloads only the objects under top/foo, but it keeps the full S3 path below destDir.
The command aws s3 cp --recursive behaves in a different way: it removes the prefix.
To remove the prefix, use a Copy task after the download:
def localTree = 'path/to/some/location'
task downloadRecursive(type: org.infosmid.gradle.s3.S3Download) {
bucket = 's3-bucket-name'
keyPrefix = "${localTree}"
destDir = "${buildDir}/download-root"
}
// Copy the downloaded tree to a new location and remove the keyPrefix.
task copyDownload(type: Copy, dependsOn: downloadRecursive) {
from "${buildDir}/download-root/${localTree}"
into "${buildDir}/pruned-tree"
}A download task reports progress in percent at the Gradle INFO log level.
Run Gradle with the -i option to see it.
Use Java 17 or later.
./gradlew buildThis command compiles the plugin and runs the unit tests and the functional tests. The functional tests do not connect to AWS.
To build the documentation and the API docs, run:
./gradlew docsReleased under the MIT License.