Skip to content

Commit

Permalink
Handle move for directories, ie move all S3 objects with prefix of pa…
Browse files Browse the repository at this point in the history
…th of directory
  • Loading branch information
jonesde committed Jul 22, 2018
1 parent 84a49fd commit 3c2d71c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/groovy/org/moqui/aws/S3ResourceReference.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,23 @@ class S3ResourceReference extends BaseResourceReference {
try {
if (autoCreateBucket && bucketName != newBucketName && !s3Client.doesBucketExistV2(newBucketName)) s3Client.createBucket(newBucketName)

// FUTURE: handle source version somehow, maybe different move or copy method? pass as third parameter to CopyObjectRequest constructor
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, path, newBucketName, newPath)
s3Client.copyObject(copyObjRequest)
s3Client.deleteObject(bucketName, path)
// if this is a file move directly, if a directory move all files with its prefix
if ((knownDirectory != null && !knownDirectory.booleanValue()) || s3Client.doesObjectExist(bucketName, path)) {
// FUTURE: handle source version somehow, maybe different move or copy method? pass as third parameter to CopyObjectRequest constructor
s3Client.copyObject(bucketName, path, newBucketName, newPath)
s3Client.deleteObject(bucketName, path)
} else {
ListObjectsV2Request lor = new ListObjectsV2Request().withBucketName(bucketName).withPrefix(path + "/")
ListObjectsV2Result result = s3Client.listObjectsV2(lor)
// objects (files in directory and all sub-directories)
List<S3ObjectSummary> objectList = result.getObjectSummaries()
for (S3ObjectSummary s3os in objectList) {
String srcPath = s3os.getKey()
String destPath = srcPath.replace(path, newPath)
s3Client.copyObject(bucketName, srcPath, newBucketName, destPath)
s3Client.deleteObject(bucketName, srcPath)
}
}
} catch (AmazonS3Exception e) {
if (e.getStatusCode() == 404) {
logger.warn("Not found (404) error in move for bucket ${bucketName} path ${path}: ${e.toString()}")
Expand Down

0 comments on commit 3c2d71c

Please sign in to comment.