From ecc4e9c97532eb790ef3eb59da704668f6b25566 Mon Sep 17 00:00:00 2001 From: Alex MacCaw Date: Thu, 2 Jul 2015 15:39:54 -0700 Subject: [PATCH] Add support for requesting directories --- lib/s3/bucket.rb | 14 ++++++++++++++ lib/s3/parser.rb | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/s3/bucket.rb b/lib/s3/bucket.rb index f8ea8c1..8df1a59 100644 --- a/lib/s3/bucket.rb +++ b/lib/s3/bucket.rb @@ -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 + # * params - additional params for list_directories + # request. + def directories(params = {}) + Proxy.new(lambda { list_directories(params) }, :owner => self) + end + def inspect #:nodoc: "#<#{self.class}:#{name}>" end @@ -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 diff --git a/lib/s3/parser.rb b/lib/s3/parser.rb index f0c6f86..f023af3 100644 --- a/lib/s3/parser.rb +++ b/lib/s3/parser.rb @@ -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)