Skip to content

Commit ef4994e

Browse files
Marcel Hekmanennoruijters
authored andcommitted
Implement copying directories.
1 parent f6f9e34 commit ef4994e

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/main/java/org/carlspring/cloud/storage/s3fs/S3FileSystemProvider.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -846,34 +846,41 @@ public void copy(Path source,
846846

847847
S3Path s3Source = toS3Path(source);
848848
S3Path s3Target = toS3Path(target);
849-
// TODO: implements support for copying directories
850-
851-
Preconditions.checkArgument(!Files.isDirectory(source), "copying directories is not yet supported: %s", source);
852-
Preconditions.checkArgument(!Files.isDirectory(target), "copying directories is not yet supported: %s", target);
853849

854850
ImmutableSet<CopyOption> actualOptions = ImmutableSet.copyOf(options);
855851
verifySupportedOptions(EnumSet.of(StandardCopyOption.REPLACE_EXISTING), actualOptions);
856852

857-
if (exists(s3Target) && !actualOptions.contains(StandardCopyOption.REPLACE_EXISTING))
853+
if (exists(s3Target))
858854
{
859-
throw new FileAlreadyExistsException(format("target already exists: %s", target));
855+
if (!actualOptions.contains(StandardCopyOption.REPLACE_EXISTING))
856+
throw new FileAlreadyExistsException(format("target already exists: %s", target));
857+
858+
if (Files.isDirectory(source))
859+
delete(s3Target);
860860
}
861861

862-
String bucketNameOrigin = s3Source.getFileStore().name();
863-
String keySource = s3Source.getKey();
864-
String bucketNameTarget = s3Target.getFileStore().name();
865-
String keyTarget = s3Target.getKey();
866-
final S3Client client = s3Source.getFileSystem().getClient();
862+
if (Files.isDirectory(source))
863+
{
864+
createDirectory(s3Target);
865+
}
866+
else
867+
{
868+
String bucketNameOrigin = s3Source.getFileStore().name();
869+
String keySource = s3Source.getKey();
870+
String bucketNameTarget = s3Target.getFileStore().name();
871+
String keyTarget = s3Target.getKey();
872+
final S3Client client = s3Source.getFileSystem().getClient();
867873

868-
final String encodedUrl = encodeUrl(bucketNameOrigin, keySource);
874+
final String encodedUrl = encodeUrl(bucketNameOrigin, keySource);
869875

870-
final CopyObjectRequest request = CopyObjectRequest.builder()
871-
.copySource(encodedUrl)
872-
.destinationBucket(bucketNameTarget)
873-
.destinationKey(keyTarget)
874-
.build();
876+
final CopyObjectRequest request = CopyObjectRequest.builder()
877+
.copySource(encodedUrl)
878+
.destinationBucket(bucketNameTarget)
879+
.destinationKey(keyTarget)
880+
.build();
875881

876-
client.copyObject(request);
882+
client.copyObject(request);
883+
}
877884
}
878885

879886
private String encodeUrl(final String bucketNameOrigin,

0 commit comments

Comments
 (0)