mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(s3): return CommonPrefixes in ListObjects when delimiter is specified (#133)
* fix(s3): return CommonPrefixes in ListObjects when delimiter is specified ListObjects with a delimiter silently dropped nested keys instead of grouping them into CommonPrefixes entries. Refactor the delimiter logic to properly separate direct objects from common prefixes and include them in the XML response, matching real S3 ListObjectsV2 behavior. docs(s3): add reference link for using prefixes in ListObjects * refactor(s3): following copilot review, sort common prefixes and all objects in ListObjects response. Update integration tests to reflect changes in object ordering. * feat(s3): implement maxKeys limit for ListObjects with delimiter as suggested by copilot in the scope of CommonPrefix fix. Added a test to cover it * fix(s3): enhance delimiter handling in ListObjects to correctly separate direct objects and common prefixes. Update logic to maintain maxKeys behavior and ensure proper sorting of results, aligning with S3 ListObjectsV2 specifications. * fix(s3): update ListObjectsResult to include isTruncated flag and adjust XML response in S3Controller to reflect this change. Enhance tests to verify truncation behavior aligns with S3 ListObjectsV2 specifications. * fix(s3): ensure Prefix element in ListBucketResult is not null by providing an empty string fallback when prefix is null following test results in floci-compatibility-tests
This commit is contained in:
1 parent
4529823557
commit
845ac85363
4 files changed
+121
-16
No files matched your search
@@ -263,20 +263,22 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
int max = (maxKeys != null && maxKeys > 0) ? maxKeys : 1000;
|
||||
List<S3Object> objects = s3Service.listObjects(bucket, prefix, delimiter, max);
|
||||
S3Service.ListObjectsResult result = s3Service.listObjectsWithPrefixes(bucket, prefix, delimiter, max);
|
||||
List<S3Object> objects = result.objects();
|
||||
List<String> commonPrefixes = result.commonPrefixes();
|
||||
boolean v2 = "2".equals(listType);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("ListBucketResult", AwsNamespaces.S3)
|
||||
.elem("Name", bucket)
|
||||
.elem("Prefix", prefix)
|
||||
.elem("Prefix", prefix != null ? prefix : "")
|
||||
.elem("Delimiter", delimiter)
|
||||
.elem("MaxKeys", max);
|
||||
if (v2) {
|
||||
xml.elem("KeyCount", objects.size());
|
||||
xml.elem("KeyCount", objects.size() + commonPrefixes.size());
|
||||
}
|
||||
xml.elem("IsTruncated", false);
|
||||
xml.elem("IsTruncated", result.isTruncated());
|
||||
for (S3Object obj : objects) {
|
||||
xml.start("Contents")
|
||||
.elem("Key", obj.getKey())
|
||||
@@ -286,6 +288,11 @@ public class S3Controller {
|
||||
.elem("StorageClass", obj.getStorageClass())
|
||||
.end("Contents");
|
||||
}
|
||||
for (String cp : commonPrefixes) {
|
||||
xml.start("CommonPrefixes")
|
||||
.elem("Prefix", cp)
|
||||
.end("CommonPrefixes");
|
||||
}
|
||||
xml.end("ListBucketResult");
|
||||
return Response.ok(xml.build()).build();
|
||||
} catch (AwsException e) {
|
||||
|
||||
@@ -439,7 +439,13 @@ public class S3Service {
|
||||
}
|
||||
}
|
||||
|
||||
public record ListObjectsResult(List<S3Object> objects, List<String> commonPrefixes, boolean isTruncated) {}
|
||||
|
||||
public List<S3Object> listObjects(String bucketName, String prefix, String delimiter, int maxKeys) {
|
||||
return listObjectsWithPrefixes(bucketName, prefix, delimiter, maxKeys).objects();
|
||||
}
|
||||
|
||||
public ListObjectsResult listObjectsWithPrefixes(String bucketName, String prefix, String delimiter, int maxKeys) {
|
||||
ensureBucketExists(bucketName);
|
||||
|
||||
String keyPrefix = bucketName + "/";
|
||||
@@ -453,21 +459,59 @@ public class S3Service {
|
||||
.toList();
|
||||
allObjects = new ArrayList<>(allObjects);
|
||||
|
||||
// see https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-prefixes.html
|
||||
List<String> commonPrefixes = List.of();
|
||||
|
||||
if (delimiter != null && !delimiter.isEmpty()) {
|
||||
// Filter to only return objects at this level (simulate directory listing)
|
||||
allObjects = allObjects.stream()
|
||||
.filter(obj -> {
|
||||
String remainder = obj.getKey().substring(prefix != null ? prefix.length() : 0);
|
||||
return !remainder.contains(delimiter);
|
||||
})
|
||||
.toList();
|
||||
Set<String> prefixSet = new LinkedHashSet<>();
|
||||
List<S3Object> directObjects = new ArrayList<>();
|
||||
|
||||
for (S3Object obj : allObjects) {
|
||||
String remainder = obj.getKey().substring(prefix != null ? prefix.length() : 0);
|
||||
int delimIdx = remainder.indexOf(delimiter);
|
||||
if (delimIdx >= 0) {
|
||||
String cp = (prefix != null ? prefix : "") + remainder.substring(0, delimIdx + delimiter.length());
|
||||
prefixSet.add(cp);
|
||||
} else {
|
||||
directObjects.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
allObjects = directObjects;
|
||||
commonPrefixes = new ArrayList<>(prefixSet);
|
||||
Collections.sort(commonPrefixes);
|
||||
}
|
||||
|
||||
if (maxKeys > 0 && allObjects.size() > maxKeys) {
|
||||
allObjects = allObjects.subList(0, maxKeys);
|
||||
allObjects.sort(Comparator.comparing(S3Object::getKey));
|
||||
|
||||
// S3 counts both direct objects and common prefixes.
|
||||
// Each common prefix group (e.g. "docs/") uses one entry regardless of
|
||||
// how many keys it contains. Merge both sorted lists lexicographically
|
||||
// and stop at maxKeys to try to match S3 ListObjectsV2 behavior.
|
||||
// see https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
|
||||
boolean isTruncated = false;
|
||||
if (maxKeys > 0) {
|
||||
List<S3Object> limitedObjects = new ArrayList<>();
|
||||
List<String> limitedPrefixes = new ArrayList<>();
|
||||
int count = 0;
|
||||
int directObjectCount = 0;
|
||||
int commonPrefixCount = 0;
|
||||
while (count < maxKeys && (directObjectCount < allObjects.size() || commonPrefixCount < commonPrefixes.size())) {
|
||||
String objectKey = directObjectCount < allObjects.size() ? allObjects.get(directObjectCount).getKey() : null;
|
||||
String prefixKey = commonPrefixCount < commonPrefixes.size() ? commonPrefixes.get(commonPrefixCount) : null;
|
||||
if (objectKey != null && (prefixKey == null || objectKey.compareTo(prefixKey) <= 0)) {
|
||||
limitedObjects.add(allObjects.get(directObjectCount++));
|
||||
} else {
|
||||
limitedPrefixes.add(commonPrefixes.get(commonPrefixCount++));
|
||||
}
|
||||
count++;
|
||||
}
|
||||
isTruncated = directObjectCount < allObjects.size() || commonPrefixCount < commonPrefixes.size();
|
||||
allObjects = limitedObjects;
|
||||
commonPrefixes = limitedPrefixes;
|
||||
}
|
||||
|
||||
return allObjects;
|
||||
return new ListObjectsResult(allObjects, commonPrefixes, isTruncated);
|
||||
}
|
||||
|
||||
public S3Object copyObject(String sourceBucket, String sourceKey,
|
||||
|
||||
Reference in new issue
Block a user