Class: Google::Cloud::Storage::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/storage/extension.rb

Instance Method Summary collapse

Instance Method Details

#f(filename, mode: 0) {|path| ... } ⇒ Object

downloads the specified file and passes the path of the temporary file to the block argument

Parameters:

  • filename (String)

Yields:

  • (path)

Yield Parameters:

  • path (String)

    path of tempfile



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
# File 'lib/google/cloud/storage/extension.rb', line 15

def f(filename, mode: 0)
  tries = 0
  begin
    file = self.file(filename)
  rescue StandardError
    tries += 1
    if tries <= 10
      sleep 0.1
      retry
    end

    raise
  end
  raise 'file not found' unless file

  name = file.name
  basename = ::File.basename(name, '.*')
  extname = ::File.extname(name)
  tempfile = ::Tempfile.create([basename, extname], mode:)

  tries = 0
  begin
    file.download(tempfile.path)
  rescue StandardError
    tries += 1
    if tries <= 10
      sleep 0.1
      retry
    end

    ::File.delete(tempfile.path)
    raise
  end

  ret = yield(tempfile.path)
  ::File.delete(tempfile.path)

  ret
end