Class: Nab::S3Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/nab/adapters/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path = ".") ⇒ S3Adapter

Returns a new instance of S3Adapter.



7
8
9
10
# File 'lib/nab/adapters/s3.rb', line 7

def initialize(base_path = ".")
  AWS.config
  @base_path = "."
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



5
6
7
# File 'lib/nab/adapters/s3.rb', line 5

def base_path
  @base_path
end

Instance Method Details

#get(uri) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nab/adapters/s3.rb', line 12

def get(uri)
  bucket_name = uri.host
  asset_name  = uri.path.gsub(/^\//, "") # remove leading slashes
  name        = File.basename asset_name
  remote_path = File.dirname asset_name
  local_path  = File.join @base_path,remote_path

  Log.debug "Bucket: #{bucket_name}"
  Log.debug "Asset name: #{asset_name}"
  Log.debug "Local path: #{local_path}"

  FileUtils.mkdir_p local_path

  s3     = AWS::S3.new
  bucket = s3.buckets[bucket_name]
  asset  = bucket.objects[asset_name]

  Log.info "Downloading #{File.join(bucket_name,asset_name)} to #{File.join(local_path,name)}"
  File.open(File.join(local_path,name), 'wb') do |file|
    asset.read do |chunk|
      file.write chunk
    end
  end
end