Skip to content

Commit

Permalink
chore(rewrite): Use S3Path.getBucketName (#758)
Browse files Browse the repository at this point in the history
* chore(rewrite): Use S3Path.getBucketName instead of S3Path.getFileStore().getName()
* chore(unused): Remove unused method in FileStoreTest.java
  • Loading branch information
guicamest authored Sep 28, 2023
1 parent 8e9b332 commit 05fb2d7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ else if (!exists && !this.options.contains(StandardOpenOption.CREATE_NEW) &&
{
final S3Client client = path.getFileSystem().getClient();
final GetObjectRequest request = GetObjectRequest.builder()
.bucket(path.getFileStore().name())
.bucket(path.getBucketName())
.key(key)
.build();
try (ResponseInputStream<GetObjectResponse> byteStream = client.getObject(request))
Expand Down Expand Up @@ -476,7 +476,7 @@ protected void sync()
{
final PutObjectRequest.Builder builder = PutObjectRequest.builder();
final long length = Files.size(tempFile);
builder.bucket(path.getFileStore().name())
builder.bucket(path.getBucketName())
.key(path.getKey())
.contentLength(length)
.contentType(new Tika().detect(stream, path.getFileName().toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public InputStream newInputStream(Path path,
{
final S3Path s3Path = toS3Path(path);
final String key = s3Path.getKey();
final String bucketName = s3Path.getFileStore().name();
final String bucketName = s3Path.getBucketName();

Preconditions.checkArgument(options.length == 0,
"OpenOptions not yet supported: %s",
Expand Down Expand Up @@ -689,7 +689,7 @@ public void createDirectory(Path dir,

// create bucket if necessary
final Bucket bucket = s3Path.getFileStore().getBucket();
final String bucketName = s3Path.getFileStore().name();
final String bucketName = s3Path.getBucketName();

if (bucket == null)
{
Expand Down Expand Up @@ -717,7 +717,7 @@ public void delete(Path path)
{
final S3Path rootPath = toS3Path(path);
final S3Client client = rootPath.getFileSystem().getClient();
final String bucketName = rootPath.getFileStore().name();
final String bucketName = rootPath.getBucketName();

LinkedList<Deque<S3Path>> s3Paths = getPathsByBatch(rootPath);

Expand Down Expand Up @@ -868,9 +868,9 @@ public void copy(Path source,
throw new FileAlreadyExistsException(format("target already exists: %s", target));
}

String bucketNameOrigin = s3Source.getFileStore().name();
String bucketNameOrigin = s3Source.getBucketName();
String keySource = s3Source.getKey();
String bucketNameTarget = s3Target.getFileStore().name();
String bucketNameTarget = s3Target.getBucketName();
String keyTarget = s3Target.getKey();
final S3Client client = s3Source.getFileSystem().getClient();

Expand Down Expand Up @@ -954,7 +954,7 @@ public void checkAccess(Path path,
{
final S3Object s3Object = s3Utils.getS3Object(s3Path);
final String key = s3Object.key();
final String bucket = s3Path.getFileStore().name();
final String bucket = s3Path.getBucketName();
final S3AccessControlList accessControlList = new S3AccessControlList(bucket, key);

accessControlList.checkAccess(modes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected void sync()
builder.contentType(new Tika().detect(stream, path.getFileName().toString()));
}

builder.bucket(path.getFileStore().name());
builder.bucket(path.getBucketName());
builder.key(path.getKey());
builder.cacheControl(requestCacheControlHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class S3Utils
public List<S3Object> listS3Objects(S3Path s3Path)
{
final String key = s3Path.getKey();
final String bucketName = s3Path.getFileStore().name();
final String bucketName = s3Path.getBucketName();
final S3Client client = s3Path.getFileSystem().getClient();

// is a virtual directory
Expand Down Expand Up @@ -89,7 +89,7 @@ public S3Object getS3Object(S3Path s3Path)
throws NoSuchFileException
{
final String key = s3Path.getKey();
final String bucketName = s3Path.getFileStore().name();
final String bucketName = s3Path.getBucketName();

final S3Client client = s3Path.getFileSystem().getClient();

Expand Down Expand Up @@ -177,7 +177,7 @@ public S3PosixFileAttributes getS3PosixFileAttributes(final S3Path s3Path)
final S3Object object = getS3Object(s3Path);

final String key = s3Path.getKey();
final String bucketName = s3Path.getFileStore().name();
final String bucketName = s3Path.getBucketName();

final S3BasicFileAttributes attrs = toS3FileAttributes(object, key);

Expand Down
12 changes: 6 additions & 6 deletions src/test/java/org/carlspring/cloud/storage/s3fs/FilesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void virtualDirectoryStreamTest()
// upload file without paths
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
final RequestBody requestBody = RequestBody.fromInputStream(inputStream, inputStream.available());
String bucketName = s3Path.getFileStore().name();
String bucketName = s3Path.getBucketName();
PutObjectRequest request = PutObjectRequest.builder().bucket(bucketName).key(file1).build();
client.putObject(request, requestBody);

Expand Down Expand Up @@ -366,7 +366,7 @@ void virtualDirectoryStreamWithVirtualSubFolderTest()
// upload without paths
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
final RequestBody requestBody = RequestBody.fromInputStream(inputStream, inputStream.available());
String bucketName = s3Path.getFileStore().name();
String bucketName = s3Path.getBucketName();
PutObjectRequest request = PutObjectRequest.builder().bucket(bucketName).key(subFolder).build();
client.putObject(request, requestBody);

Expand Down Expand Up @@ -532,7 +532,7 @@ void createFileWithFolderAndNotExistsFolders()
// upload without paths
final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
final RequestBody requestBody = RequestBody.fromInputStream(inputStream, inputStream.available());
String bucketName = s3Path.getFileStore().name();
String bucketName = s3Path.getBucketName();
PutObjectRequest request = PutObjectRequest.builder().bucket(bucketName).key(fileWithFolders).build();
client.putObject(request, requestBody);

Expand All @@ -555,7 +555,7 @@ void amazonCopyDetectContentType()
Files.copy(htmlFile, result);

final S3Path resultS3 = (S3Path) result;
final String bucketName = resultS3.getFileStore().name();
final String bucketName = resultS3.getBucketName();
final String key = resultS3.getKey();
final HeadObjectRequest request = HeadObjectRequest.builder().bucket(bucketName).key(key).build();
final HeadObjectResponse response = resultS3.getFileSystem()
Expand Down Expand Up @@ -597,7 +597,7 @@ void amazonCopyNotDetectContentTypeSetDefault()
Files.copy(htmlFile, result);

final S3Path resultS3 = (S3Path) result;
final String bucketName = resultS3.getFileStore().name();
final String bucketName = resultS3.getBucketName();
final String key = resultS3.getKey();
final HeadObjectRequest request = HeadObjectRequest.builder().bucket(bucketName).key(key).build();
final HeadObjectResponse response = resultS3.getFileSystem()
Expand Down Expand Up @@ -639,7 +639,7 @@ void amazonOutputStreamDetectContentType()
}

final S3Path resultS3 = (S3Path) result;
final String bucketName = resultS3.getFileStore().name();
final String bucketName = resultS3.getBucketName();
final String key = resultS3.getKey();
final HeadObjectRequest request = HeadObjectRequest.builder().bucket(bucketName).key(key).build();
final HeadObjectResponse response = resultS3.getFileSystem()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void getRootDirectories()
{
S3Path s3Path = (S3Path) path;

String fileStore = s3Path.getFileStore().name();
String fileStore = s3Path.getBucketName();

Path fileName = s3Path.getFileName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void createNoPath()
{
S3Path path = forPath("/bucket");

assertEquals("bucket", path.getFileStore().name());
assertEquals("bucket", path.getBucketName());
assertEquals("", path.getKey());
}

Expand All @@ -51,7 +51,7 @@ void createWithTrailingSlash()
{
S3Path path = forPath("/bucket/");

assertEquals(path.getFileStore().name(), "bucket");
assertEquals(path.getBucketName(), "bucket");
assertEquals(path.getKey(), "");
}

Expand All @@ -60,7 +60,7 @@ void createWithPath()
{
S3Path path = forPath("/bucket/path/to/file");

assertEquals(path.getFileStore().name(), "bucket");
assertEquals(path.getBucketName(), "bucket");
assertEquals(path.getKey(), "path/to/file");
}

Expand All @@ -69,7 +69,7 @@ void createWithPathAndTrailingSlash()
{
S3Path path = forPath("/bucket/path/to/dir/");

assertEquals("bucket", path.getFileStore().name());
assertEquals("bucket", path.getBucketName());
assertEquals("path/to/dir/", path.getKey());
}

Expand All @@ -78,7 +78,7 @@ void createWithPathAndTrailingSlashDir()
{
S3Path path = forPath("/bucket/path/to/dir/");

assertEquals("bucket", path.getFileStore().name());
assertEquals("bucket", path.getBucketName());
assertEquals("path/to/dir/", path.getKey());
}

Expand Down Expand Up @@ -266,24 +266,24 @@ void constructors()
S3FileSystem fileSystem = s3fsProvider.getFileSystem(S3EndpointConstant.S3_GLOBAL_URI_TEST);
S3Path path = new S3Path(fileSystem, "/buckname");

assertEquals("buckname", path.getFileStore().name());
assertEquals("buckname", path.getBucketName());
assertEquals("", path.getKey());
assertNull(path.getParent());
assertEquals("", path.getKey());

path = new S3Path(fileSystem, "/buckname/");

assertEquals("buckname", path.getFileStore().name());
assertEquals("buckname", path.getBucketName());
assertEquals("", path.getKey());

path = new S3Path(fileSystem, "/buckname/file");

assertEquals("buckname", path.getFileStore().name());
assertEquals("buckname", path.getBucketName());
assertEquals("file", path.getKey());

path = new S3Path(fileSystem, "/buckname/dir/file");

assertEquals("buckname", path.getFileStore().name());
assertEquals("buckname", path.getBucketName());
assertEquals("dir/file", path.getKey());

path = new S3Path(fileSystem, "dir/file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,4 @@ void getFileStore()
}
}

// ~ helper methods

private Path get(String path)
{
return fs.getPath(path);
}

}

0 comments on commit 05fb2d7

Please sign in to comment.