Module: VagrantPlugins::S3Auth::Util

Defined in:
lib/vagrant-s3auth/util.rb

Constant Summary collapse

S3_HOST_MATCHER =
/^((?<bucket>[[:alnum:]\-\.]+).)?s3([[:alnum:]\-\.]+)?\.amazonaws\.com$/
LOCATION_TO_REGION =
Hash.new { |_, key| key }.merge(
  nil => 'us-east-1',
  'EU' => 'eu-west-1'
)

Class Method Summary collapse

Class Method Details

.get_bucket_region(bucket) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/vagrant-s3auth/util.rb', line 47

def self.get_bucket_region(bucket)
  LOCATION_TO_REGION[::AWS::S3.new.buckets[bucket].location_constraint]
rescue ::AWS::S3::Errors::AccessDenied
  raise Errors::BucketLocationAccessDeniedError,
    bucket: bucket,
    access_key: ENV['AWS_ACCESS_KEY_ID']
end

.s3_object_for(url, follow_redirect = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant-s3auth/util.rb', line 16

def self.s3_object_for(url, follow_redirect = true)
  url = URI(url)

  if url.scheme == 's3'
    bucket = url.host
    key = url.path[1..-1]
    raise Errors::MalformedShorthandURLError, url: url unless bucket && key
  elsif match = S3_HOST_MATCHER.match(url.host)
    components = url.path.split('/').delete_if(&:empty?)
    bucket = match['bucket'] || components.shift
    key = components.join('/')
  end

  if bucket && key
    ::AWS::S3.new(region: get_bucket_region(bucket))
      .buckets[bucket].objects[key]
  elsif follow_redirect
    response = Net::HTTP.get_response(url) rescue nil
    if response.is_a?(Net::HTTPRedirection)
      s3_object_for(response['location'], false)
    end
  end
end

.s3_url_for(method, s3_object) ⇒ Object



40
41
42
43
44
45
# File 'lib/vagrant-s3auth/util.rb', line 40

def self.s3_url_for(method, s3_object)
  s3_object.url_for(method,
    expires: 10,
    signature_version: :v4,
    force_path_style: true)
end