Class: Longleaf::S3UriHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/helpers/s3_uri_helper.rb

Overview

Helper for interacting with s3 uris

Constant Summary collapse

ENDPOINT_PATTERN =
/^(.+\.)?s3[.\-]([a-z0-9\-]+[\-.])?[a-z0-9]+\./
ALLOWED_SCHEMES =
['http', 'https', 's3']

Class Method Summary collapse

Class Method Details

.extract_bucket(url) ⇒ Object

Extract the name of the s3 bucket from the provided url

Parameters:

  • url

    s3 url

Returns:

  • the name of the bucket, or nil if the name could not be identified



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/longleaf/helpers/s3_uri_helper.rb', line 12

def self.extract_bucket(url)
  uri = s3_uri(url)

  matches = ENDPOINT_PATTERN.match(uri.host)
  if matches.nil?
    raise ArgumentError.new("Provided URI does match the expected pattern for an S3 URI")
  end

  prefix = matches[1]
  if prefix.nil? || prefix.empty?
    # Is a path style url
    path = uri.path

    return nil if path == '/'

    path_parts = path.split('/')
    return nil if path_parts.empty?
    return path_parts[1]
  else
    return prefix[0..-2]
  end
end

.extract_path(url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/longleaf/helpers/s3_uri_helper.rb', line 35

def self.extract_path(url)
  uri = s3_uri(url)

  matches = ENDPOINT_PATTERN.match(uri.host)
  if matches.nil?
    raise ArgumentError.new("Provided URI does match the expected pattern for an S3 URI")
  end

  path = uri.path
  return nil if path == '/' || path.empty?

  # trim off the first slash
  path = path.partition('/').last

  # Determine if the first part of the path is the bucket name
  prefix = matches[1]
  if prefix.nil? || prefix.empty?
    # trim off the bucket name
    path = path.partition('/').last
  end

  path
end

.extract_region(url) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/longleaf/helpers/s3_uri_helper.rb', line 59

def self.extract_region(url)
  uri = s3_uri(url)

  matches = ENDPOINT_PATTERN.match(uri.host)

  if matches[2].nil?
    # No region specified
    nil
  else
    matches[2][0..-2]
  end
end

.s3_uri(url) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/longleaf/helpers/s3_uri_helper.rb', line 72

def self.s3_uri(url)
  if url.nil?
    raise ArgumentError.new("url cannot be empty")
  end
  uri = URI(url)
  if !ALLOWED_SCHEMES.include?(uri.scheme&.downcase)
    raise ArgumentError.new("Invalid scheme for s3 URI #{url}, only http, https and s3 are permitted")
  end
  if uri.host.nil?
    raise ArgumentError.new("Invalid S3 URI, no hostname: #{url}")
  end
  uri
end