Class: Awshucks::FileStore

Inherits:
Object show all
Defined in:
lib/awshucks/file_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_info, bucket_name, prefix) ⇒ FileStore

Returns a new instance of FileStore.



7
8
9
10
# File 'lib/awshucks/file_store.rb', line 7

def initialize(connection_info, bucket_name, prefix)
  @bucket_name, @prefix = bucket_name, prefix
  AWS::S3::Base.establish_connection!(connection_info) unless AWS::S3::Base.connected?
end

Instance Method Details

#different_from?(file_info) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/awshucks/file_store.rb', line 16

def different_from?(file_info)
  return true unless info = (file_info.filename)
  if info['mtime'] == file_info.mtime.gmtime.to_s
    false
  else
    if info['md5sum'] == file_info.md5sum
      info['mtime'] = file_info.mtime.gmtime.to_s # update the metadata cache
      false
    else
      true
    end
  end
end

#each_fileObject



12
13
14
# File 'lib/awshucks/file_store.rb', line 12

def each_file
  bucket.objects(:prefix => prefix).each { |f| yield remove_prefix(f.key) unless f.key ==  }
end

#reset_cacheObject



53
54
55
56
57
# File 'lib/awshucks/file_store.rb', line 53

def reset_cache
  puts "deleting the metadata cache..."
  AWS::S3::S3Object.delete(, bucket.name)
  @metadata = {}
end

#restore(filename, target) ⇒ Object

stores the value of the file stored on S3 to the target file

Raises:



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/awshucks/file_store.rb', line 37

def restore(filename, target)
  raise UnknownFileError, "unknown file #{filename}" unless file = bucket[prefixed_filename(filename)]
  FileUtils.mkdir_p(File.dirname(target))
  File.open(target, 'w') do |out|
    file.value do |data|
      out.write(data)
    end
  end
  # warn if the file's md5 doesn't match after writing it out
  $stderr.puts "warning: md5sums don't match" if (filename)['md5sum'] != File.md5sum(target)
end

#save_cacheObject



49
50
51
# File 'lib/awshucks/file_store.rb', line 49

def save_cache
  AWS::S3::S3Object.store(, .to_yaml, bucket.name)
end

#store(file_info) ⇒ Object



30
31
32
33
34
# File 'lib/awshucks/file_store.rb', line 30

def store(file_info)
  [file_info.filename] = { 'mtime' => file_info.mtime.to_s, 'md5sum' => file_info.md5sum }
   = { 'x-amz-meta-mtime' => file_info.mtime.to_s, 'x-amz-meta-md5sum' => file_info.md5sum }
  AWS::S3::S3Object.store(prefixed_filename(file_info.filename), file_info.data, bucket.name, ).success?
end