Sha256: fea0a448ff4d0aab5e1934f4b30e764b9966c64bc723ff6841c97ffa6317ea27

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

require 'aws-sdk-s3'
require 'uri'

class Lono::Bundler::Component::Fetcher
  class S3 < Base
    extend Memoist

    def run
      region, bucket, key, path = s3_info
      download(region, bucket, key, path)
    end

    def download(region, bucket, key, path)
      # Download to cache area
      response_target = cache_path(path) # temporary path

      unless File.exist?(response_target)
        logger.debug "S3 save archive to #{response_target}".color(:yellow)
        FileUtils.mkdir_p(File.dirname(response_target))
        s3_get(region, response_target, bucket, key)
      end

      # Save to stage
      dest = stage_path(rel_dest_dir)
      extract(response_target, dest)
    end

  private
    def s3_get(region, response_target, bucket, key)
      s3(region).get_object(
        response_target: response_target,
        bucket: bucket,
        key: key,
      )
    rescue Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NoSuchKey => e
      logger.error(<<~EOL.color(:red))
        ERROR: #{e.class} #{e.message}

            bucket: #{bucket}
            key: #{key}

      EOL
      exit 1
    end

    def s3_info
      path = type_path
      bucket, key = get_bucket_key(path)

      url = @component.source.sub('s3::','')
      uri = URI(url)
      region = if uri.host == 'https://s3.amazonaws.com'
                 'us-east-1'
               else
                 uri.host.match(/s3-(.*)\.amazonaws/)[1]
               end

      [region, bucket, key, path]
    end

    def s3(region)
      Aws::S3::Client.new(region: region)
    end
    memoize :s3
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lono-8.0.0.pre.rc6 lib/lono/bundler/component/fetcher/s3.rb
lono-8.0.0.pre.rc5 lib/lono/bundler/component/fetcher/s3.rb
lono-8.0.0.pre.rc4 lib/lono/bundler/component/fetcher/s3.rb
lono-8.0.0.pre.rc3 lib/lono/bundler/component/fetcher/s3.rb
lono-8.0.0.pre.rc2 lib/lono/bundler/component/fetcher/s3.rb
lono-8.0.0.pre.rc1 lib/lono/bundler/component/fetcher/s3.rb