Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no longer throw exception on set times #361

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/examples/java/software/amazon/nio/spi/examples/MoveObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.nio.spi.examples;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Demonstrates a move operation using the `Files` class
*/
public class MoveObject {

private static final Logger logger = LoggerFactory.getLogger(MoveObject.class.getName());

public static void main(String[] args) throws IOException {

if (args.length != 2) {
logger.error("Usage: java MoveObject <source> <destination>");
System.exit(1);
}

logger.info("Moving {} to {}", args[0], args[1]);

URI source = URI.create(args[0]);
URI destination = URI.create(args[1]);
Files.move(Path.of(source), Path.of(destination));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.nio.spi.s3.util.TimeOutUtils;

class S3BasicFileAttributeView implements BasicFileAttributeView {

private final Logger logger = LoggerFactory.getLogger("S3BasicFileAttributeView");

private final S3Path path;

S3BasicFileAttributeView(S3Path path) {
Expand All @@ -22,7 +26,7 @@ class S3BasicFileAttributeView implements BasicFileAttributeView {

/**
* Returns the name of the attribute view. Attribute views of this type
* have the name {@code "basic"}.
* have the name {@code "s3"}.
*/
@Override
public String name() {
Expand All @@ -43,12 +47,15 @@ public BasicFileAttributes readAttributes() throws IOException {
}

/**
* Unsupported operation, write operations are not supported.
* S3 doesn't support setting of file times other than by writing the file. Therefore, this operation does
* nothing (no-op). To support {@code Files.copy()} and {@code Files.move()} operations which call this method,
* we don't throw an exception. The time set during those operations will be determined by S3.
*/
@Override
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) {
throw new UnsupportedOperationException(
"write operations are not supported, please submitted a feature request explaining your use case");
// intentional no-op. S3 doesn't support setting of file times other than by writing the file.
logger.warn("S3 doesn't support setting of file times other than by writing the file. " +
"The time set during those operations will be determined by S3. This method call will be ignored");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.nio.spi.s3;

import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;
import org.junit.jupiter.api.Test;

class S3BasicFileAttributeViewTest {
final String uriString = "s3://mybucket";
final S3FileSystemProvider provider = new S3FileSystemProvider();

S3FileSystem fileSystem = provider.getFileSystem(URI.create(uriString), true);
S3Path path = S3Path.getPath(fileSystem, uriString);
S3BasicFileAttributeView view = new S3BasicFileAttributeView(path);

@Test
void setTimes() {
assertDoesNotThrow(() -> view.setTimes(null, null, null));
}

@Test
void name() {
assertEquals("s3", view.name());
}
}