Class: MLS::CLI::Storage::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/mls/cli/storage/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}) ⇒ S3

Returns a new instance of S3.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/mls/cli/storage/s3.rb', line 6

def initialize(configs = {})
  @configs = configs
  @configs[:region] ||= 'us-east-1'
  @configs[:prefix] ||= ""
  
  @client = Aws::S3::Client.new({
    access_key_id: configs[:access_key_id],
    secret_access_key: configs[:secret_access_key],
    region: configs[:region]
  })
end

Instance Method Details

#copy_to_tempfile(key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mls/cli/storage/s3.rb', line 66

def copy_to_tempfile(key)
  tmpfile = Tempfile.new([File.basename(key), File.extname(key)], binmode: true)
  cp(key, tmpfile.path)
  if block_given?
    begin
      yield(tmpfile)
    ensure
      tmpfile.close!
    end
  else
    tmpfile
  end
end

#cp(key, path) ⇒ Object



57
58
59
60
# File 'lib/mls/cli/storage/s3.rb', line 57

def cp(key, path)
  object_for(destination(key)).get({ response_target: path })
  true
end

#delete(key) ⇒ Object



62
63
64
# File 'lib/mls/cli/storage/s3.rb', line 62

def delete(key)
  object_for(destination(key)).delete
end

#destination(key) ⇒ Object



35
36
37
# File 'lib/mls/cli/storage/s3.rb', line 35

def destination(key)
  "#{@configs[:prefix]}#{partition(key)}".gsub(/^\//, '')#delete_prefix('/')
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mls/cli/storage/s3.rb', line 39

def exists?(key)
  object_for(destination(key)).exists?
end

#hostObject



30
31
32
33
# File 'lib/mls/cli/storage/s3.rb', line 30

def host
  h = @configs[:bucket_host_alias] || "https://s3.amazonaws.com/#{@configs[:bucket]}"
  h.delete_suffix('/')
end

#last_modified(key) ⇒ Object



90
91
92
# File 'lib/mls/cli/storage/s3.rb', line 90

def last_modified(key)
  object_for(destination(key)).last_modified
end

#local?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/mls/cli/storage/s3.rb', line 22

def local?
  false
end

#md5(key) ⇒ Object



86
87
88
# File 'lib/mls/cli/storage/s3.rb', line 86

def md5(key)
  object_for(destination(key)).etag
end

#mime_type(key) ⇒ Object



94
95
96
# File 'lib/mls/cli/storage/s3.rb', line 94

def mime_type(key)
  object_for(destination(key)).content_type
end

#object_for(key) ⇒ Object



18
19
20
# File 'lib/mls/cli/storage/s3.rb', line 18

def object_for(key)
  Aws::S3::Object.new(@configs[:bucket], key, client: @client)
end

#partition(value) ⇒ Object



80
81
82
83
84
# File 'lib/mls/cli/storage/s3.rb', line 80

def partition(value)
  return value unless @configs[:partition]
  split = value.scan(/.{1,4}/)
  split.shift(@configs[:partition_depth] || 3).join("/") + split.join("")
end

#read(key, &block) ⇒ Object

def write(key, file, meta_info)

file = file.tempfile if file.is_a?(ActionDispatch::Http::UploadedFile)

object_for(destination(key)).upload_file(file, {
  :acl => 'public-read',
  :content_disposition => "inline; filename=\"#{meta_info[:filename]}\"",
  :content_type => meta_info[:content_type]
})

end



53
54
55
# File 'lib/mls/cli/storage/s3.rb', line 53

def read(key, &block)
  object_for(destination(key)).get()
end

#url(key) ⇒ Object



26
27
28
# File 'lib/mls/cli/storage/s3.rb', line 26

def url(key)
  [host, destination(key)].join('/')
end