Class: Eco::API::Common::Session::S3Uploader

Inherits:
Object
  • Object
show all
Includes:
Language::AuxiliarLogger
Defined in:
lib/eco/api/common/session/s3_uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Constructor Details

#initialize(enviro:) ⇒ S3Uploader

Returns a new instance of S3Uploader.



11
12
13
14
15
16
17
18
# File 'lib/eco/api/common/session/s3_uploader.rb', line 11

def initialize(enviro:)
  msg = "Required Environment object (enviro:). Given: #{enviro.class}"
  raise msg if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)

  @enviro    = enviro
  @prefix    = fetch_prefix
  @timestamp = Time.now.iso8601
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/eco/api/common/session/s3_uploader.rb', line 8

def prefix
  @prefix
end

Instance Method Details

Returns link to the S3 object on console.

Parameters:

  • path (String)

    a full path to a S3 object

Returns:

  • (String)

    link to the S3 object on console



66
67
68
69
70
71
72
73
# File 'lib/eco/api/common/session/s3_uploader.rb', line 66

def link(path)
  return path.map { |pth| link(pth) } if path.is_a?(Enumerable)
  return unless path.is_a?(String)

  s3_path_str = "https://s3.console.aws.amazon.com/s3/object"
  s3_params   = "region=#{fetch_region}&tab=overview"
  "#{s3_path_str}/#{path.sub('s3://', '')}?#{s3_params}"
end

#upload(filename, content) ⇒ String

Uploads content to S3 as filename

Parameters:

  • filename (String)

    the name of the object to be created on S3

  • content (String)

    that to be uploaded

Returns:

  • (String)

    S3 path to the uploaded filename object



24
25
26
27
28
29
30
31
32
# File 'lib/eco/api/common/session/s3_uploader.rb', line 24

def upload(filename, content)
  if (obj = new_s3_object(filename))
    log_upload(obj) do
      obj.put(body: content)
    end
  end

  full_path(obj)
end

#upload_directory(path, recurse: false) ⇒ Array<String>

Note:

it will skip subfolders

Returns S3 paths to all the uploaded files of path directory.

Parameters:

  • path (String)

    the target directory to be uploaded

  • recurse (Boolean) (defaults to: false)

    deepen in the folder structure? (false: default)

Returns:

  • (Array<String>)

    S3 paths to all the uploaded files of path directory



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/eco/api/common/session/s3_uploader.rb', line 49

def upload_directory(path, recurse: false)
  path      = File.expand_path(path)
  prefix    = File.expand_path(File.join(path, ".."))
  wildcard  = recurse ? "**/*" : "*"

  Dir.glob(File.join(path, wildcard)).sort.map do |file|
    next unless File.file?(file) # Skip directories
    key = file.sub(prefix, "").gsub(/\\/, "/").sub(/^\/+/, "")

    File.open(file, "rb") do |f|
      upload(key, f)
    end
  end.compact
end

#upload_file(path) ⇒ String

Uploads a single file

Parameters:

  • path (String)

    the target file to be uploaded

Returns:

  • (String)

    S3 path to the uploaded path file



37
38
39
40
41
42
43
# File 'lib/eco/api/common/session/s3_uploader.rb', line 37

def upload_file(path)
  return unless File.exist?(path)

  File.open(path, "rb") do |f|
    upload(File.basename(path), f)
  end
end