Skip to content

Instantly share code, notes, and snippets.

@fleveque
Created February 4, 2016 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleveque/e50d2768c0f77c5c42b1 to your computer and use it in GitHub Desktop.
Save fleveque/e50d2768c0f77c5c42b1 to your computer and use it in GitHub Desktop.
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