Class: Amazon::S3::Uri::AmazonS3URI

Inherits:
Object
  • Object
show all
Defined in:
lib/uri.rb

Constant Summary collapse

ENDPOINT_PATTERN =
/^(.+\.)?s3[.-]([a-z0-9-]+)\./

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ AmazonS3URI

Returns a new instance of AmazonS3URI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uri.rb', line 10

def initialize(url)
  if url.nil?
    raise ArgumentError.new("url cannot be null")
  end
  @uri = URI(url)

  @host = @uri.host
  if @host.nil?
    raise ArgumentError.new("Invalid S3 URI: no hostname: " + url)
  end

  matches = ENDPOINT_PATTERN.match(@host)

  prefix = matches[1]

  if prefix.nil? || prefix.empty?
    # No bucket name in the authority; parse it from the path.
    @isPathStyle = true
    path = uri.path
    if (path == '/')
      @bucket = nil
      @key = nil
    else
      # path genearlly in style of `/sample-bucket/temp/8746ee3e-4089-11e8-9a0b-f3d94c494e17.somaya.jpg`
      index = path.index('/', 1)
      if index.nil?
        # path if equals /sample-bucket
        # puts path
        @bucket = path[1 .. -1]
        @key = nil
      elsif index == path.length - 1
        # path if equals /sample-bucket/
        @bucket = path[1 ... index]
        @key = nil
      else
        # path if equals `/sample-bucket/temp/8746ee3e-4089-11e8-9a0b-f3d94c494e17.somaya.jpg`
        @bucket = path[1 ... index]
        @key = path[index + 1 .. -1]
      end
    end
  else
    @isPathStyle = false

    # Remove the trailing '.' from the prefix to get the bucket.
    @bucket = prefix[0 ... -1]
    path = uri.path
    if path.nil? || path.empty? || path  == '/'
      @key = nil
    else
      # Remove the leading '/'
      @key = path[ 1 .. -1]
    end
  end

  if matches[2] == 'amazonaws'
    # No region specified
    @region = nil
  else
    @region = matches[2]
  end
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/uri.rb', line 9

def bucket
  @bucket
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/uri.rb', line 9

def key
  @key
end

#regionObject (readonly)

Returns the value of attribute region.



9
10
11
# File 'lib/uri.rb', line 9

def region
  @region
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/uri.rb', line 9

def uri
  @uri
end