Sha256: e17d0b7bf59196cacae181b28fda29521d3f63776aaa0184127e943274f3af3e

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

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

class TerraspaceBundler::Mod::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 = @mod.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
terraspace-bundler-0.5.0 lib/terraspace_bundler/mod/fetcher/s3.rb
terraspace-bundler-0.4.4 lib/terraspace_bundler/mod/fetcher/s3.rb
terraspace-bundler-0.4.3 lib/terraspace_bundler/mod/fetcher/s3.rb
terraspace-bundler-0.4.2 lib/terraspace_bundler/mod/fetcher/s3.rb
terraspace-bundler-0.4.1 lib/terraspace_bundler/mod/fetcher/s3.rb
terraspace-bundler-0.4.0 lib/terraspace_bundler/mod/fetcher/s3.rb