Skip to content

Instantly share code, notes, and snippets.

@fleveque
Created February 4, 2016 10:13
Returns an array of subfolders for a given path on S3, with aws-sdk (ruby)
# Returns an array of subfolders for a given path
def folders(path = '/')
objs = []
next_marker = nil
loop do
response = Aws::S3::Client.new.list_objects(
bucket: 'bucket',
delimiter: "/",
encoding_type: "url",
marker: next_marker,
prefix: File.join(path, "") # we need trailing slash
)
next_marker = response.next_marker
objs += response.common_prefixes.map { |cp| File.basename(cp.prefix) }
break unless response.is_truncated
end
objs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment