Class: Mamiya::Storages::S3

Inherits:
Abstract show all
Defined in:
lib/mamiya/storages/s3.rb

Direct Known Subclasses

S3Proxy

Defined Under Namespace

Classes: MultipleObjectsDeletionError

Instance Attribute Summary

Attributes inherited from Abstract

#application, #config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #prune

Constructor Details

This class inherits a constructor from Mamiya::Storages::Abstract

Class Method Details

.find(config = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mamiya/storages/s3.rb', line 21

def self.find(config={})
  s3 = initiate_s3_with_config(config)
  Hash[s3.list_objects(bucket: config[:bucket], delimiter: '/').common_prefixes.map { |prefix|
    app = prefix.prefix.gsub(%r{/$},'')
    [app, self.new(config.merge(application: app))]
  }]
end

.initiate_s3_with_config(config) ⇒ Object

:nodoc:



117
118
119
# File 'lib/mamiya/storages/s3.rb', line 117

def self.initiate_s3_with_config(config) # :nodoc:
  Aws::S3::Client.new(s3_config(config))
end

.s3_config(base) ⇒ Object

:nodoc:



121
122
123
124
125
126
127
# File 'lib/mamiya/storages/s3.rb', line 121

def self.s3_config(base) # :nodoc:
  base.dup.tap do |c|
    c.delete(:bucket)
    c.delete(:application)
    c.delete(:type)
  end
end

Instance Method Details

#fetch(package_name, dir) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mamiya/storages/s3.rb', line 57

def fetch(package_name, dir)
  package_key, meta_key = package_and_meta_key_for(package_name)

  package_path = File.join(dir, File.basename(package_key))
  meta_path = File.join(dir, File.basename(meta_key))

  if File.exists?(package_path) && File.exists?(meta_path)
    raise AlreadyFetched
  end

  open(package_path, 'wb+') do |io|
    s3.get_object({bucket: @config[:bucket], key: package_key}, target: io)
  end
  open(meta_path, 'wb+') do |io|
    s3.get_object({bucket: @config[:bucket], key: meta_key}, target: io)
  end

  return Mamiya::Package.new(package_path)

rescue AlreadyFetched, NotFound => e
  raise e

rescue Aws::S3::Errors::NoSuchKey
  File.unlink package_path if package_path && File.exists?(package_path)
  File.unlink meta_path if meta_path && File.exists?(meta_path)

  raise NotFound

rescue Exception => e
  File.unlink package_path if package_path && File.exists?(package_path)
  File.unlink meta_path if meta_path && File.exists?(meta_path)

  raise e
end

#meta(package_name) ⇒ Object



92
93
94
95
96
97
# File 'lib/mamiya/storages/s3.rb', line 92

def meta(package_name)
  _, meta_key = package_and_meta_key_for(package_name)
  JSON.parse(s3.get_object(bucket: @config[:bucket], key: meta_key).body.string)
rescue Aws::S3::Errors::NoSuchKey
  return nil
end

#packagesObject



29
30
31
32
33
34
35
36
37
# File 'lib/mamiya/storages/s3.rb', line 29

def packages
  s3.list_objects(bucket: @config[:bucket], delimiter: '/', prefix: "#{self.application}/").contents.map { |content|
    content.key.sub(/\A#{Regexp.escape(self.application)}\//, '')
  }.group_by { |key|
    key.sub(Package::PATH_SUFFIXES,'')
  }.select { |key, files|
    files.find { |file| file.end_with?('.tar.gz') } && files.find { |file| file.end_with?('.json') }
  }.keys
end

#push(package) ⇒ Object

Raises:

  • (TypeError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mamiya/storages/s3.rb', line 39

def push(package)
  raise TypeError, "package should be a kind of Mamiya::Package" unless package.kind_of?(Mamiya::Package)
  raise NotBuilt, "package not built" unless package.exists?

  package_key, meta_key = package_and_meta_key_for(package.name)

  [package_key, meta_key].each do |key|
    raise AlreadyExists if key_exists_in_s3?(key)
  end

  open(package.path, 'rb') do |io|
    s3.put_object(bucket: @config[:bucket], key: package_key, body: io)
  end
  open(package.meta_path, 'rb') do |io|
    s3.put_object(bucket: @config[:bucket], key: meta_key, body: io)
  end
end

#remove(package_name) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mamiya/storages/s3.rb', line 99

def remove(package_name)
  package_key, meta_key = package_and_meta_key_for(package_name)

  objs_to_delete = [package_key, meta_key].map { |key|
    if key_exists_in_s3?(key)
      {key: key}
    else
      nil
    end
  }.compact
  raise NotFound if objs_to_delete.empty?

  result = s3.delete_objects(bucket: @config[:bucket], delete: {objects: objs_to_delete})
  unless result.errors.empty?
    raise MultipleObjectsDeletionError.new(result.errors)
  end
end