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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enviro:) ⇒ S3Uploader

Returns a new instance of S3Uploader.



9
10
11
12
13
14
# File 'lib/eco/api/common/session/s3_uploader.rb', line 9

def initialize (enviro:)
  raise "Required Environment object (enviro:). Given: #{enviro}" 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.



6
7
8
# File 'lib/eco/api/common/session/s3_uploader.rb', line 6

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



60
61
62
63
64
65
66
67
# File 'lib/eco/api/common/session/s3_uploader.rb', line 60

def link(path)
  unless path.is_a?(Enumerable)
    return nil unless path.is_a?(String)
    return "https://s3.console.aws.amazon.com/s3/object/#{path.sub("s3://","")}?region=#{fetch_region}&tab=overview"
   return link
 end
 path.map {|pth| link(pth)}
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



20
21
22
23
24
25
26
27
# File 'lib/eco/api/common/session/s3_uploader.rb', line 20

def upload(filename, content)
  if obj = new_s3_object(filename)
    log_upload(obj) do
      obj.put(body: content)
    end
  end
  return 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



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/eco/api/common/session/s3_uploader.rb', line 44

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



32
33
34
35
36
37
38
# File 'lib/eco/api/common/session/s3_uploader.rb', line 32

def upload_file(path)
  if File.exist?(path)
    File.open(path, "rb") do |f|
      upload(File.basename(path), f)
    end
  end
end