Class: DB2S3::S3Store

Inherits:
Object
  • Object
show all
Defined in:
lib/db2s3.rb

Instance Method Summary collapse

Constructor Details

#initializeS3Store

Returns a new instance of S3Store.



132
133
134
# File 'lib/db2s3.rb', line 132

def initialize
  @connected = false
end

Instance Method Details

#delete(file_name) ⇒ Object



164
165
166
167
168
169
# File 'lib/db2s3.rb', line 164

def delete(file_name)
  if object = bucket.objects.find(file_name)
    puts "Deleting #{file_name}"
    object.destroy
  end
end

#ensure_connectedObject



136
137
138
139
140
141
# File 'lib/db2s3.rb', line 136

def ensure_connected
  return if @connected
  s3_service = S3::Service.new(DB2S3::Config::S3.slice(:access_key_id, :secret_access_key).merge(:use_ssl => true))
  @bucket = s3_service.buckets.build(DB2S3::Config::S3[:bucket])
  @connected = true
end

#fetch(file_name) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/db2s3.rb', line 150

def fetch(file_name)
  ensure_connected
  file = Tempfile.new('dump')
  file.binmode if file.respond_to?(:binmode)
  file.write(bucket.objects.find(file_name).content)
  file.rewind
  file
end

#list(prefix) ⇒ Object



159
160
161
162
# File 'lib/db2s3.rb', line 159

def list(prefix)
  ensure_connected
  bucket.objects.find_all(:prefix => prefix).collect {|x| x.key }
end

#store(file_name, file) ⇒ Object



143
144
145
146
147
148
# File 'lib/db2s3.rb', line 143

def store(file_name, file)
  ensure_connected
  object = bucket.objects.build(file_name)
  object.content = file.class == String ? file : (file.rewind; file.read)
  object.save
end