Skip to content

Instantly share code, notes, and snippets.

@gyli
Created September 23, 2019 21:10
Show Gist options
  • Save gyli/735b4714d2b0939efad35e63ddda8799 to your computer and use it in GitHub Desktop.
Save gyli/735b4714d2b0939efad35e63ddda8799 to your computer and use it in GitHub Desktop.
CopyObjectRecursively
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