mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
60 lines
1.5 KiB
Ruby
60 lines
1.5 KiB
Ruby
require 'fog/collection'
|
|
require 'fog/aws/models/s3/directory'
|
|
|
|
module Fog
|
|
module AWS
|
|
module S3
|
|
|
|
class Real
|
|
def directories
|
|
Fog::AWS::S3::Directories.new(:connection => self)
|
|
end
|
|
end
|
|
|
|
class Mock
|
|
def directories
|
|
Fog::AWS::S3::Directories.new(:connection => self)
|
|
end
|
|
end
|
|
|
|
class Directories < Fog::Collection
|
|
|
|
model Fog::AWS::S3::Directory
|
|
|
|
def all
|
|
data = connection.get_service.body['Buckets']
|
|
load(data)
|
|
end
|
|
|
|
def get(name, options = {})
|
|
remap_attributes(options, {
|
|
:delimiter => 'delimiter',
|
|
:marker => 'marker',
|
|
:max_keys => 'max-keys',
|
|
:prefix => 'prefix'
|
|
})
|
|
data = connection.get_bucket(name, options).body
|
|
directory = new(:name => data['Name'])
|
|
options = {}
|
|
for key, value in data
|
|
if ['Delimiter', 'IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
|
options[key] = value
|
|
end
|
|
end
|
|
directory.files.merge_attributes(options)
|
|
files = data['Contents']
|
|
while data['IsTruncated']
|
|
data = connection.get_bucket(name, options.merge!('marker' => files.last['Key'])).body
|
|
files.concat(data['Contents'])
|
|
end
|
|
directory.files.load(files)
|
|
directory
|
|
rescue Excon::Errors::NotFound
|
|
nil
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|