Created
November 26, 2013 18:51
-
-
Save trevorrowe/7663726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree | |
def initialize(name = nil, path = '') | |
@name = name | |
@path = path | |
@folders = {} | |
@files = [] | |
end | |
attr_reader :name, :path, :folders, :files | |
def add_file(obj) | |
file = extract_data(obj) | |
folder_for(file['path']).files << file | |
end | |
def to_h | |
if @name | |
{ | |
"name" => @name, | |
"isDir" => true, | |
"path" => @path, | |
"children" => children | |
} | |
else | |
children | |
end | |
end | |
private | |
def children | |
@folders.values.map(&:to_h) + @files | |
end | |
def extract_data(obj) | |
{ | |
"name" => File.basename(obj[:key]), | |
"isFile" => true, | |
"path" => obj[:key], | |
"meta" => { | |
"last_modified" => obj[:last_modified].utc.strftime('%Y-%m-%dT%H:%M:%S+00:00'), | |
"content_type" => nil, # must call #head_object to get this or guess it | |
"content_length" => obj[:size].to_i, | |
} | |
} | |
end | |
def folder_for(path) | |
File.dirname(path).split('/').inject(self) do |tree, folder_name| | |
tree.folders[folder_name] ||= Tree.new(folder_name, tree.path + folder_name + '/') | |
end | |
end | |
end | |
def process_keys(resp, tree) | |
resp[:contents].each do |obj| | |
tree.add_file(obj) | |
end | |
end | |
options = { bucket_name: 'my-bucket-name' } | |
s3 = AWS::S3::Client.new | |
resp = s3.list_objects(options) | |
tree = Tree.new | |
process_keys(resp, tree) | |
# you must request the next page of results until you have all objects | |
while resp[:truncated] | |
resp = s3.list_objects(options.merge(:marker => resp[:contents].last[:key])) | |
process_keys(resp, tree) | |
end | |
tree.to_h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment