Class: Cnvrg::Downloader::Clients::GcpClient

Inherits:
Cnvrg::Downloader::Client show all
Defined in:
lib/cnvrg/downloader/clients/gcp_client.rb

Instance Method Summary collapse

Methods inherited from Cnvrg::Downloader::Client

#cut_prefix, #decrypt, #extract_key_iv, factory, #link_file, #mkdir, #prepare_download, #safe_download, #safe_operation, #safe_upload

Constructor Details

#initialize(project_id: nil, credentials: nil, bucket_name: nil, sts: nil) ⇒ GcpClient

Returns a new instance of GcpClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cnvrg/downloader/clients/gcp_client.rb', line 7

def initialize(project_id: nil, credentials: nil, bucket_name: nil, sts: nil)
  @key, @iv = extract_key_iv(sts)
  @project_id = Cnvrg::Helpers.decrypt(@key, @iv, project_id) if project_id.present?
  @credentials_path = Cnvrg::Helpers.decrypt(@key, @iv, credentials)
  @tempfile = nil
  @bucket_name = Cnvrg::Helpers.decrypt(@key, @iv, bucket_name)
  init_gcp_credentials
  @storage = Google::Cloud::Storage.new(project_id: @project_id, credentials: @credentials, retries: 50, timeout: 43200)
  @bucket = @storage.bucket(@bucket_name)
  @bucket.name
rescue => e
  Cnvrg::Logger.log_error(e)
  Cnvrg::Logger.log_info("Tried to init gcp client without success.")
  Cnvrg::CLI.log_message("Cannot init client. please contact support to check your bucket credentials.")
  exit(1)
end

Instance Method Details

#download(storage_path, local_path, decrypt: true) ⇒ Object



34
35
36
37
38
# File 'lib/cnvrg/downloader/clients/gcp_client.rb', line 34

def download(storage_path, local_path, decrypt: true)
  prepare_download(local_path)
  file = @bucket.file(decrypt(storage_path))
  file.download local_path
end

#init_gcp_credentialsObject



24
25
26
27
28
29
30
31
32
# File 'lib/cnvrg/downloader/clients/gcp_client.rb', line 24

def init_gcp_credentials
  t = Tempfile.new
  f = open(@credentials_path).read
  t.binmode
  t.write(f)
  t.rewind
  @credentials = t.path
  @tempfile = t
end

#upload(storage_path, local_path) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cnvrg/downloader/clients/gcp_client.rb', line 40

def upload(storage_path, local_path)
  begin
    @bucket.create_file(local_path, storage_path)
  rescue => e
    raise e
  end
end