File: directories.rb

package info (click to toggle)
ruby-fog-google 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: ruby: 16,775; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,432 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
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
40
41
module Fog
  module Storage
    class GoogleJSON
      class Directories < Fog::Collection
        model Fog::Storage::GoogleJSON::Directory

        def all(opts = {})
          data = service.list_buckets(**opts).to_h[:items] || []
          load(data)
        end

        def get(bucket_name, options = {})
          if_metageneration_match = options[:if_metageneration_match]
          if_metageneration_not_match = options[:if_metageneration_not_match]
          projection = options[:projection]

          data = service.get_bucket(
            bucket_name,
            :if_metageneration_match => if_metageneration_match,
            :if_metageneration_not_match => if_metageneration_not_match,
            :projection => projection
          ).to_h

          directory = new(data)
          # Because fog-aws accepts these arguments on files at the
          # directories.get level, we need to preload the directory files
          # with these attributes here.
          files_attr_names = %i(delimiter page_token max_results prefix)

          file_opts = options.select { |k, _| files_attr_names.include? k }
          directory.files(file_opts)
          directory
        rescue ::Google::Apis::ClientError => e
          # metageneration check failures returns HTTP 412 Precondition Failed
          raise e unless e.status_code == 404 || e.status_code == 412
          nil
        end
      end
    end
  end
end