diff --git a/src/main/java/software/amazon/nio/spi/s3/S3WritableByteChannel.java b/src/main/java/software/amazon/nio/spi/s3/S3WritableByteChannel.java index c41fd820..17459f76 100644 --- a/src/main/java/software/amazon/nio/spi/s3/S3WritableByteChannel.java +++ b/src/main/java/software/amazon/nio/spi/s3/S3WritableByteChannel.java @@ -1,5 +1,6 @@ package software.amazon.nio.spi.s3; +import org.slf4j.Logger; import software.amazon.awssdk.services.s3.S3AsyncClient; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectRequest; @@ -13,7 +14,12 @@ import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.channels.WritableByteChannel; -import java.nio.file.*; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -31,6 +37,8 @@ class S3WritableByteChannel implements WritableByteChannel { private boolean open; + Logger logger = org.slf4j.LoggerFactory.getLogger(S3WritableByteChannel.class); + S3WritableByteChannel(S3Path path, S3AsyncClient client, Set options, Long timeout, TimeUnit timeUnit) throws IOException { Objects.requireNonNull(path); Objects.requireNonNull(client); @@ -69,7 +77,16 @@ class S3WritableByteChannel implements WritableByteChannel { } } - options.remove(StandardOpenOption.CREATE_NEW); + try { + options.remove(StandardOpenOption.CREATE_NEW); + } catch (UnsupportedOperationException e) { + if (options.isEmpty() || !options.contains(StandardOpenOption.CREATE_NEW)) { + // options is immutable but it doesn't matter because the value isn't there anyway. + logger.debug("Could not remove CREATE_NEW option as the operation is unsupported on options", e); + } else { + throw e; + } + } channel = Files.newByteChannel(this.tempFile, options); } catch (InterruptedException e) { Thread.currentThread().interrupt();