Class: Eco::API::Common::Session::S3Uploader
- Includes:
- Language::AuxiliarLogger
- Defined in:
- lib/eco/api/common/session/s3_uploader.rb
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
-
#initialize(enviro:) ⇒ S3Uploader
constructor
A new instance of S3Uploader.
-
#link(path) ⇒ String
link
to the S3 object on console. -
#upload(filename, content) ⇒ String
Uploads
content
to S3 asfilename
. -
#upload_directory(path, recurse: false) ⇒ Array<String>
S3 paths to all the uploaded files of
path
directory. -
#upload_file(path) ⇒ String
Uploads a single file.
Methods included from Language::AuxiliarLogger
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
#prefix ⇒ Object (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
#link(path) ⇒ String
Returns 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
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.
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.(path) prefix = File.(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
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 |