Class: Eco::API::Common::Session::S3Uploader
- 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.
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
#prefix ⇒ Object (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
#link(path) ⇒ String
Returns 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
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.
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.(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
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 |