8
9
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
|
# File 'lib/open-gcs.rb', line 8
def self.open_gcs(uri, *rest, **options, &block)
scheme, bucket, path = get_path_info(uri)
unless scheme == TARGET_SCHEME
raise(ArgumentError, "Invalid uri: #{uri} (This method is only support for gcs object uri.")
end
if String === rest.first || Integer === rest.first
mode = rest.first
else
mode = options[:mode]
end
if !mode.nil? && !mode.match?(/\Ar[bt]?(?:\Z|:([^:]+))/) && mode != ::File::RDONLY
raise(ArgumentError, "Invalid access mode: #{mode}. (This method is only support for read mode.)")
end
begin
tf = File.download(bucket, path, **options.select { |key| File::OPTION_KEYS.include?(key) })
file = ::File.open(tf.path, *rest, **options.reject { |key| File::OPTION_KEYS.include?(key) })
if block_given?
begin
yield file
ensure
tf.close!
end
else
file
end
ensure
tf&.close if tf&.path
end
end
|