diff --git a/stdlib/aws/s3/s3.cue b/stdlib/aws/s3/s3.cue index 485755a6..99e8a20e 100644 --- a/stdlib/aws/s3/s3.cue +++ b/stdlib/aws/s3/s3.cue @@ -90,3 +90,80 @@ import ( ] } } + +// S3 Sync +#Sync: { + // AWS Config + config: aws.#Config + + // Source Artifact to upload to S3 + source: dagger.#Artifact @dagger(input) + + // Target S3 URL (eg. s3:////) + target: string @dagger(input) + + // Files that exist in the destination but not in the + // source are deleted during sync. + delete: *false | bool @dagger(input) + + // Object content type + contentType: string | *"" @dagger(input) + + // Always write the object to S3 + always: bool | *true @dagger(input) + + // URL of the uploaded S3 object + url: { + @dagger(output) + string + + #up: [ + op.#Load & { + from: aws.#CLI & { + "config": config + } + }, + + op.#Exec & { + if always != _|_ { + "always": always + } + env: { + TARGET: target + OPT_CONTENT_TYPE: contentType + if delete { + OPT_DELETE: "1" + } + } + + mount: "/source": from: source + + args: [ + "/bin/bash", + "--noprofile", + "--norc", + "-eo", + "pipefail", + "-c", + #""" + opts=() + if [ -d /source ]; then + op=sync + fi + [ -n "$OPT_CONTENT_TYPE" ] && opts+="--content-type $OPT_CONTENT_TYPE" + [ -n "$OPT_DELETE" ] && opts+="--delete" + aws s3 sync ${opts[@]} /source "$TARGET" + echo -n "$TARGET" \ + | sed -E 's=^s3://([^/]*)/=https://\1.s3.amazonaws.com/=' \ + > /url + """#, + ] + }, + + op.#Export & { + source: "/url" + format: "string" + }, + ] + } +} diff --git a/tests/stdlib/aws/s3/.dagger/env/default/plan/s3.cue b/tests/stdlib/aws/s3/.dagger/env/default/plan/s3.cue index e5a9f690..8968493c 100644 --- a/tests/stdlib/aws/s3/.dagger/env/default/plan/s3.cue +++ b/tests/stdlib/aws/s3/.dagger/env/default/plan/s3.cue @@ -49,3 +49,23 @@ TestS3UploadDir: { file: "foo.txt" } } + +TestS3Sync: { + deploy: s3.#Sync & { + config: TestConfig.awsConfig + source: TestDirectory + target: "s3://\(bucket)/" + } + + verifyFile: #VerifyS3 & { + config: TestConfig.awsConfig + target: deploy.target + file: "dirFile.txt" + } + + verifyDir: #VerifyS3 & { + config: TestConfig.awsConfig + target: deploy.target + file: "foo.txt" + } +}