class Eco::API::Common::Session::S3Uploader
Attributes
prefix[R]
Public Class Methods
new(enviro:)
click to toggle source
@param enviro [Eco::API::Common::Session::Environment]
# File lib/eco/api/common/session/s3_uploader.rb, line 11 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
Public Instance Methods
link(path)
click to toggle source
@param path [String] a full path to a S3 object @return [String] `link` to the S3 object on console
# 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)
click to toggle source
Uploads `content` to S3 as `filename` @param filename [String] the name of the object to be created on S3 @param content [String] that to be uploaded @return [String] S3 path to the uploaded `filename` object
# File lib/eco/api/common/session/s3_uploader.rb, line 22 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)
click to toggle source
@note it will skip subfolders @param path [String] the target directory to be uploaded @param recurse [Boolean] deepen in the folder structure? (`false`: default) @return [Array<String>] S3 paths to all the uploaded files of `path` directory
# 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)
click to toggle source
Uploads a single file @param path [String] the target file to be uploaded @return [String] S3 path to the uploaded `path` file
# File lib/eco/api/common/session/s3_uploader.rb, line 34 def upload_file(path) File.open(path, "rb") do |f| upload(File.basename(path), f) end end
Private Instance Methods
bucket()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 75 def bucket begin @bucket ||= Aws::S3::Resource.new( access_key_id: fetch_access_key_id, secret_access_key: fetch_secret_access_key, region: fetch_region ).bucket(fetch_bucket) rescue Exception => e logger.error("Trying to upload to S3 with wrong configuration: #{e}") end @bucket end
config()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 106 def config @enviro.config || {} end
fetch_access_key_id()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 118 def fetch_access_key_id config.s3storage.access_key_id || ENV['AWS_ACCESS_KEY_ID'] end
fetch_bucket()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 110 def fetch_bucket config.s3storage.bucket_name end
fetch_prefix()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 114 def fetch_prefix config.s3storage.prefix end
fetch_region()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 126 def fetch_region config.s3storage.region || ENV['AWS_REGION'] end
fetch_secret_access_key()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 122 def fetch_secret_access_key config.s3storage.secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'] end
full_path(obj)
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 71 def full_path(obj) return "s3://#{bucket.name}/#{obj.key}" if obj end
log_upload(obj) { || ... }
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 97 def log_upload(obj) yield logger.debug("Uploaded #{full_path(obj)}") end
logger()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 102 def logger @enviro&.logger || ::Logger.new(IO::NULL) end
new_s3_object(filename)
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 92 def new_s3_object(filename) file_path = [session_path, filename].join("/") bucket&.object(file_path) end
session_path()
click to toggle source
# File lib/eco/api/common/session/s3_uploader.rb, line 88 def session_path ["scripts", @prefix, @timestamp].join("/") end