Class: VagrantPlugins::BoxS3::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-box-s3/utils.rb

Constant Summary collapse

S3_URL_HOST_REGEX =
%r{^https?://([\w\-\.]+)\.s3[-\.]([\w\-]+)\.amazonaws\.com/([^?]+)}
S3_URL_PATH_REGEX =
%r{^https?://s3[-\.]([\w\-]+)\.amazonaws\.com/([^/]+)/([^?]+)}

Class Method Summary collapse

Class Method Details

.is_s3_manifest(url) ⇒ Object

Check if the URL is an S3 URL and the filename is ‘manifest.json’



29
30
31
32
33
34
# File 'lib/vagrant-box-s3/utils.rb', line 29

def self.is_s3_manifest(url)
  uri = URI.parse(url)
  filename = File.basename(uri.path)

  return is_s3_url(url) && filename == 'manifest.json'
end

.is_s3_url(url) ⇒ Object

Check if URL matches S3 URLs.



19
20
21
22
23
24
25
26
# File 'lib/vagrant-box-s3/utils.rb', line 19

def self.is_s3_url(url)
  # Check if the URL matches either the host style or path style S3 URL
  matches_host_style = !!(url =~ S3_URL_HOST_REGEX)
  matches_path_style = !!(url =~ S3_URL_PATH_REGEX)

  # Return true if either match is found, false otherwise
  matches_host_style || matches_path_style
end

.parse_s3_url(url) ⇒ Object

Parse an s3 URL.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant-box-s3/utils.rb', line 37

def self.parse_s3_url(url)
  region = bucket = key = nil
  if url =~ S3_URL_HOST_REGEX
    match = S3_URL_HOST_REGEX.match(url)
    region = match[2]
    bucket = match[1]
    key = match[3]
  elsif url =~ S3_URL_PATH_REGEX
    match = S3_URL_PATH_REGEX.match(url)
    region = match[1]
    bucket = match[2]
    key = match[3]
  end

  return region, bucket, key
end