Class: MP4::Source::S3

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/mp4/source/s3.rb

Constant Summary

Constants included from Base

Base::REQUIRED_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

included

Constructor Details

#initialize(bucket:, key:, client: nil) ⇒ S3

Returns a new instance of S3.



51
52
53
54
55
56
57
# File 'lib/mp4/source/s3.rb', line 51

def initialize(bucket:, key:, client: nil)
  @bucket    = bucket
  @key       = key
  @client    = client
  @position  = 0
  @file_size = nil
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



5
6
7
# File 'lib/mp4/source/s3.rb', line 5

def bucket
  @bucket
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/mp4/source/s3.rb', line 5

def key
  @key
end

Class Method Details

.from_uri(uri, client: nil) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/mp4/source/s3.rb', line 8

def from_uri(uri, client: nil)
  match = uri.match(%r{\As3://(?<bucket>[^/]+)/(?<key>.+)\z})
  raise ArgumentError, "expected s3://BUCKET/KEY URI, got #{uri.inspect}" unless match

  new(bucket: match[:bucket], key: match[:key], client: client)
end

.reset_shared_client!Object



22
23
24
# File 'lib/mp4/source/s3.rb', line 22

def reset_shared_client!
  @shared_client_mutex&.synchronize { @shared_client = nil }
end

.shared_clientObject



15
16
17
18
19
20
# File 'lib/mp4/source/s3.rb', line 15

def shared_client
  @shared_client_mutex ||= Mutex.new
  @shared_client_mutex.synchronize do
    @shared_client ||= build_client
  end
end

Instance Method Details

#closeObject



101
102
103
104
105
# File 'lib/mp4/source/s3.rb', line 101

def close
  # No per-instance connection to close; connections live in the shared
  # client's connection pool and are reused across sources.
  nil
end

#eof?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mp4/source/s3.rb', line 89

def eof?
  @position >= size
end

#fetch(from, to) ⇒ Object



77
78
79
# File 'lib/mp4/source/s3.rb', line 77

def fetch(from, to)
  get_range(from, to)
end

#open!Object



63
64
65
66
# File 'lib/mp4/source/s3.rb', line 63

def open!
  size
  self
end

#posObject



93
94
95
# File 'lib/mp4/source/s3.rb', line 93

def pos
  @position
end

#read(n) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/mp4/source/s3.rb', line 68

def read(n)
  return ''.b if eof?

  to = [@position + n - 1, size - 1].min
  data = get_range(@position, to)
  @position += data.bytesize
  data
end

#reset_seekObject



85
86
87
# File 'lib/mp4/source/s3.rb', line 85

def reset_seek
  @position = 0
end

#set_seek(n) ⇒ Object



81
82
83
# File 'lib/mp4/source/s3.rb', line 81

def set_seek(n)
  @position = n.clamp(0, size)
end

#sizeObject



97
98
99
# File 'lib/mp4/source/s3.rb', line 97

def size
  @file_size ||= client.head_object(bucket: @bucket, key: @key).content_length.to_i
end

#uriObject



59
60
61
# File 'lib/mp4/source/s3.rb', line 59

def uri
  "s3://#{@bucket}/#{@key}"
end