class Dpl::Providers::S3

Constants

DEFAULT_CONTENT_TYPE

Public Instance Methods

deploy() click to toggle source
# File lib/dpl/providers/s3.rb, line 73
def deploy
  upload
  index_document_suffix if index_document_suffix?
rescue Aws::S3::Errors::ServiceError => e
  handle_error(e)
end
finish() click to toggle source
# File lib/dpl/providers/s3.rb, line 80
def finish
  Dir.chdir(@cwd) if @cwd
end
login() click to toggle source
# File lib/dpl/providers/s3.rb, line 69
def login
  info :login
end
setup() click to toggle source
# File lib/dpl/providers/s3.rb, line 63
def setup
  @cwd = Dir.pwd
  Dir.chdir(local_dir)
  # Aws.eager_autoload!(services: ['S3'])
end

Private Instance Methods

acl() click to toggle source
Calls superclass method
# File lib/dpl/providers/s3.rb, line 147
def acl
  super.gsub(/_/, '-') if acl?
end
bucket() click to toggle source
Calls superclass method
# File lib/dpl/providers/s3.rb, line 191
def bucket
  @bucket ||= Aws::S3::Resource.new(client:).bucket(super)
end
client() click to toggle source
# File lib/dpl/providers/s3.rb, line 195
def client
  Aws::S3::Client.new(s3_opts)
end
compact(hash) click to toggle source
# File lib/dpl/providers/s3.rb, line 162
def compact(hash)
  hash.reject { |_, value| value.nil? }.to_h
end
content_type(file) click to toggle source
# File lib/dpl/providers/s3.rb, line 155
def content_type(file)
  return DEFAULT_CONTENT_TYPE unless type = MIME::Types.type_for(file).first

  type = "#{type}; charset=#{default_text_charset}" if encoding(file) == 'text' && default_text_charset?
  type.to_s
end
credentials() click to toggle source
# File lib/dpl/providers/s3.rb, line 208
def credentials
  Aws::Credentials.new(access_key_id, secret_access_key)
end
endpoint() click to toggle source
Calls superclass method
# File lib/dpl/providers/s3.rb, line 166
def endpoint
  @endpoint ||= normalize_endpoint(super) if endpoint?
end
files() click to toggle source
# File lib/dpl/providers/s3.rb, line 139
def files
  @files ||= Dir.glob(*glob).reject { |path| File.directory?(path) }
end
glob() click to toggle source
Calls superclass method
# File lib/dpl/providers/s3.rb, line 143
def glob
  [super, dot_match? ? File::FNM_DOTMATCH : nil].compact
end
handle_error(err) click to toggle source
# File lib/dpl/providers/s3.rb, line 178
def handle_error(err)
  case err
  when Aws::S3::Errors::InvalidAccessKeyId
    error :invalid_access_key_id
  when Aws::S3::Errors::ChecksumError
    error :checksum_error
  when Aws::S3::Errors::AccessDenied
    error :access_denied
  else
    error err.message
  end
end
index_document_suffix() click to toggle source
Calls superclass method
# File lib/dpl/providers/s3.rb, line 117
def index_document_suffix
  info :index_document_suffix, super
  body = { website_configuration: { index_document: { suffix: super } } }
  bucket.website.put(body)
end
match_opt(strs, file) click to toggle source
# File lib/dpl/providers/s3.rb, line 216
def match_opt(strs, file)
  maps = Array(strs).map { |str| Mapping.new(str, file) }
  maps.map(&:value).compact.first
end
normalize_endpoint(url) click to toggle source
# File lib/dpl/providers/s3.rb, line 170
def normalize_endpoint(url)
  uri = URI.parse(url)
  return uri if uri.scheme

  info :default_uri_scheme
  URI.parse("https://#{url}")
end
progress(file, data) click to toggle source
# File lib/dpl/providers/s3.rb, line 101
def progress(file, data)
  if verbose?
    info :upload_file, file, upload_dir || '/', to_pairs(data)
  else
    print '.'
  end
end
s3_opts() click to toggle source
# File lib/dpl/providers/s3.rb, line 199
def s3_opts
  compact(
    region:,
    credentials:,
    endpoint:,
    force_path_style: force_path_style?
  )
end
server_side_encryption() click to toggle source
# File lib/dpl/providers/s3.rb, line 151
def server_side_encryption
  'AES256' if server_side_encryption?
end
to_pairs(hash) click to toggle source
# File lib/dpl/providers/s3.rb, line 212
def to_pairs(hash)
  hash.map { |pair| pair.join('=') }.join(' ')
end
upload() click to toggle source
# File lib/dpl/providers/s3.rb, line 86
def upload
  info :upload, files.length, max_threads
  threads = max_threads.times.map { |_i| Thread.new(&method(:upload_files)) }
  threads.each(&:join)
  info "\n" unless verbose?
end
upload_file(file, opts) click to toggle source
# File lib/dpl/providers/s3.rb, line 109
def upload_file(file, opts)
  object = bucket.object(upload_path(file))
  return warn :upload_skipped, file: file if !overwrite && object.exists?

  info :upload_file, file, upload_dir || '/', to_pairs(opts)
  object.upload_file(file, opts) || warn(:upload_failed, file)
end
upload_files() click to toggle source
# File lib/dpl/providers/s3.rb, line 93
def upload_files
  while file = files.pop
    opts = upload_opts(file)
    progress(file, opts)
    upload_file(file, opts)
  end
end
upload_opts(file) click to toggle source
# File lib/dpl/providers/s3.rb, line 127
def upload_opts(file)
  compact(
    acl:,
    content_type: content_type(file),
    content_encoding: detect_encoding? ? encoding(file) : nil,
    cache_control: match_opt(cache_control, file),
    expires: match_opt(expires, file),
    storage_class:,
    server_side_encryption:
  )
end
upload_path(file) click to toggle source
# File lib/dpl/providers/s3.rb, line 123
def upload_path(file)
  [upload_dir, file].compact.join('/')
end