Skip to content

Commit a5296d6

Browse files
authored
[Fix-17177] Fix cos resource can not list (#17178)
1 parent 5aa2345 commit a5296d6

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

  • dolphinscheduler-storage-plugin/dolphinscheduler-storage-cos/src/main/java/org/apache/dolphinscheduler/plugin/storage/cos

dolphinscheduler-storage-plugin/dolphinscheduler-storage-cos/src/main/java/org/apache/dolphinscheduler/plugin/storage/cos/CosStorageOperator.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,44 @@ public List<String> fetchFileContent(String filePath, int skipLineNums, int limi
221221
}
222222

223223
@Override
224-
public List<StorageEntity> listStorageEntity(String resourceAbsolutePath) {
225-
resourceAbsolutePath = transformCOSKeyToAbsolutePath(resourceAbsolutePath);
224+
public List<StorageEntity> listStorageEntity(String path) {
225+
/*
226+
* If the incoming path ends with File.separator, it is assumed that the incoming intent is to access the
227+
* directory and direct query If the incoming path does not end with `File.separator`, it is considered that the
228+
* incoming intent could be either a file or a directory, and a determination needs to be made. If the object is
229+
* queried as a file, return the file; otherwise, query it as a directory.
230+
*/
231+
if (!path.endsWith(File.separator)) {
232+
boolean objectExists = cosClient.doesObjectExist(bucketName, path);
233+
if (!objectExists) {
234+
path = path + File.separator;
235+
}
236+
}
237+
238+
String resourceAbsolutePath = path;
226239

227240
ListObjectsRequest request = new ListObjectsRequest();
228241
request.setBucketName(bucketName);
229242
request.setPrefix(resourceAbsolutePath);
230243
request.setDelimiter(File.separator);
231244

232245
ObjectListing result = cosClient.listObjects(request);
233-
234-
return result.getObjectSummaries()
246+
List<StorageEntity> storageEntitys = new ArrayList<>();
247+
// add directories
248+
storageEntitys.addAll(result.getCommonPrefixes()
249+
.stream()
250+
.filter(x -> !resourceAbsolutePath.equals(x))
251+
.map(key -> {
252+
ObjectMetadata metadata = new ObjectMetadata();
253+
COSObject object = new COSObject();
254+
object.setObjectMetadata(metadata);
255+
object.setKey(key);
256+
return transformCOSObjectToStorageEntity(object);
257+
}).collect(Collectors.toList()));
258+
// add files
259+
storageEntitys.addAll(result.getObjectSummaries()
235260
.stream()
261+
.filter(x -> !x.getKey().endsWith(File.separator))
236262
.map((COSObjectSummary summary) -> {
237263
ObjectMetadata metadata = new ObjectMetadata();
238264
metadata.setContentLength(summary.getSize());
@@ -242,7 +268,8 @@ public List<StorageEntity> listStorageEntity(String resourceAbsolutePath) {
242268
object.setKey(summary.getKey());
243269
return transformCOSObjectToStorageEntity(object);
244270
})
245-
.collect(Collectors.toList());
271+
.collect(Collectors.toList()));
272+
return storageEntitys;
246273
}
247274

248275
@Override

0 commit comments

Comments
 (0)