Module: PatriotGCP::Ext::GCS

Included in:
Command::GCSCommand
Defined in:
lib/patriot_gcp/ext/gcs.rb

Defined Under Namespace

Classes: GCSException

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(cls) ⇒ Object



9
10
11
# File 'lib/patriot_gcp/ext/gcs.rb', line 9

def self.included(cls)
  cls.send(:include, Patriot::Util::System)
end

Instance Method Details

#gcs(gcs_keyfile, project_id, bucket, command, source_file, dest_file) ⇒ Object



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
# File 'lib/patriot_gcp/ext/gcs.rb', line 15

def gcs(gcs_keyfile, project_id, bucket, command, source_file, dest_file)
  ENV['GOOGLE_CLOUD_KEYFILE'] = gcs_keyfile

  storage = Google::Cloud::Storage.new(
    project: project_id,
    retries: 3, # default value
    timeout: 3600
  )

  bucket = storage.bucket bucket

  if command == 'create_file'
    bucket.create_file(source_file, dest_file)
  elsif command == 'download'
    file = bucket.file source_file

    if file.nil?
      raise GCSException, "File not found."
    end

    file.download dest_file
  elsif command == 'delete'
    file = bucket.file source_file

    if file.nil?
      @logger.info "File not found."
    else
      file.delete
    end
  end
end