Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/main/java/com/upplication/s3fs/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.spi.FileSystemProvider;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -318,7 +321,52 @@ public InputStream newInputStream(Path path, OpenOption... options) throws IOExc
}
}

@Override
@Override
public OutputStream newOutputStream(final Path path, final OpenOption... options) throws IOException {
S3Path s3Path = toS3Path(path);

// validate options
if (options.length > 0) {
Set<OpenOption> opts = new LinkedHashSet<>(Arrays.asList(options));

// cannot handle APPEND here -> use newByteChannel() implementation
if (opts.contains(StandardOpenOption.APPEND)) {
return super.newOutputStream(path, options);
}

if (opts.contains(StandardOpenOption.READ)) {
throw new IllegalArgumentException("READ not allowed");
}

boolean create = opts.remove(StandardOpenOption.CREATE);
boolean createNew = opts.remove(StandardOpenOption.CREATE_NEW);
boolean truncateExisting = opts.remove(StandardOpenOption.TRUNCATE_EXISTING);

// remove irrelevant/ignored options
opts.remove(StandardOpenOption.WRITE);
opts.remove(StandardOpenOption.SPARSE);

if (!opts.isEmpty()) {
throw new UnsupportedOperationException(opts.iterator().next() + " not supported");
}

if (!(create && truncateExisting)) {
if (s3Path.getFileSystem().provider().exists(s3Path)) {
if (createNew || !truncateExisting) {
throw new FileAlreadyExistsException(path.toString());
}
} else {
if (!createNew && !create) {
throw new NoSuchFileException(path.toString());
}
}
}
}

return new S3OutputStream(s3Path.getFileSystem().getClient(), s3Path.toS3ObjectId());
}

@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
S3Path s3Path = toS3Path(path);
return new S3SeekableByteChannel(s3Path, options);
Expand Down
Loading