Created
September 23, 2019 21:10
-
-
Save gyli/735b4714d2b0939efad35e63ddda8799 to your computer and use it in GitHub Desktop.
CopyObjectRecursively
This file contains hidden or 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
def CopyObjectRecursively( | |
s3client: AmazonS3Client, | |
sourcePath: String, | |
targetPath: String, | |
includeSourceBucketInTargetPath: Boolean = false): Unit = { | |
val sourceURI: AmazonS3URI = new AmazonS3URI(sourcePath) | |
val targetURI: AmazonS3URI = new AmazonS3URI(targetPath) | |
val sourceBucket = sourceURI.getBucket() | |
val sourcePrefix = sourceURI.getKey() | |
val targetBucket = targetURI.getBucket() | |
val targetPrefix = targetURI.getKey() | |
try { | |
val objectListingRequest = new ListObjectsRequest() | |
.withBucketName(sourceBucket) | |
.withPrefix(sourcePrefix) | |
val objectListing = amazonS3Client | |
.listObjects(objectListingRequest) | |
.getObjectSummaries | |
.asScala | |
.toList; | |
for (file <- objectListing) { | |
val copyRequest = new CopyObjectRequest( | |
sourceBucket, | |
file.getKey(), | |
targetBucket, | |
Paths | |
.get(targetPrefix, | |
if (includeSourceBucketInTargetPath) targetBucket + "/" | |
else "", | |
file.getKey()) | |
.toString() | |
).withStorageClass(StorageClass.OneZoneInfrequentAccess) | |
amazonS3Client.copyObject(copyRequest); | |
} | |
} catch { | |
case ex: Exception => | |
val exceptionmessage = ex.getMessage | |
println("ERROR: " + exceptionmessage) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment