Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for requesting directories #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/s3/bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ def object(key)
Object.send(:new, self, :key => key)
end

# Returns the root directories in the bucket and caches the result
#
# ==== Parameters
# * <tt>params</tt> - additional params for <tt>list_directories</tt>
# request.
def directories(params = {})
Proxy.new(lambda { list_directories(params) }, :owner => self)
end

def inspect #:nodoc:
"#<#{self.class}:#{name}>"
end
Expand Down Expand Up @@ -167,6 +176,11 @@ def list_bucket(params = {})
objects_attributes.map { |object_attributes| Object.send(:new, self, object_attributes) }
end

def list_directories(params = {})
response = bucket_request(:get, :params => params.merge(:delimiter => "/"))
parse_list_directories_result(response.body)
end

def bucket_headers(options = {})
bucket_request(:head, :params => options)
rescue Error::ResponseError => e
Expand Down
8 changes: 8 additions & 0 deletions lib/s3/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def parse_list_bucket_result(xml)
objects_attributes
end

def parse_list_directories_result(xml)
names = []
rexml_document(xml).elements.each("ListBucketResult/CommonPrefixes/Prefix") do |e|
names << e.text
end
names
end

def parse_copy_object_result(xml)
object_attributes = {}
document = rexml_document(xml)
Expand Down