Google Cloud Storage sort directory by name

730 views Asked by At

This feels wrong to me - given a prefix in GCS and knowing my "folders" are consistently named with a long value (e.g. a date in unix time) I want to get the first listing if i was to sort them in descending order. Right now, I only see how to iterate through them all and sort the list:

ListOptions.Builder b = new ListOptions.Builder();
    b.setRecursive(false);
    b.setPrefix(path);
    ListResult result = null;
    result = gcsService.list(appIdentity.getDefaultGcsBucketName(), ListOptions.DEFAULT);
    List<Long> names = new ArrayList<>();

    while (result.hasNext()){

        ListItem l = result.next();
        String name = l.getName();
        logger.info("get top folder" + name);
        names.add(Long.valueOf(name));

    }
     Collections.sort(names);
     long topDay = names.get(0);

Maybe a list option i don't see?

1

There are 1 answers

1
Brandon Yarbrough On BEST ANSWER

If the numbers have the same length, you are looking for the last element on the last page of the results. There is no parameter which reverses result sorting, unfortunately.

If the numbers are not the same length, that's rough. The best way to solve that would probably be to iterate through the options and keep track of the best one you've seen yet, although sorting through them all afterwards also works.