Listing just the sub folders in an s3 bucket

前端 未结 2 1927
忘了有多久
忘了有多久 2020-12-30 04:40

I have an s3 structure as follows:

s3bucketname -> List of first level keys -> List of second level keys -> List of third level keys -> Actual fi         


        
2条回答
  •  悲哀的现实
    2020-12-30 05:27

    I did the following code which seems to work fine, you have to pass a prefix and make sure the prefix ends with /, and also specify the delimiter you want to get your list of sub-directories. The following should work:

    public List listKeysInDirectory(String bucketName, String prefix) {
        String delimiter = "/";
        if (!prefix.endsWith(delimiter)) {
            prefix += delimiter;
        }
    
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(bucketName).withPrefix(prefix)
                .withDelimiter(delimiter);
        ObjectListing objects = _client.listObjects(listObjectsRequest);
        return objects.getCommonPrefixes();
    }
    

提交回复
热议问题