Method: Cnvrg::Files#download_file_s3

Defined in:
lib/cnvrg/files.rb

#download_file_s3(relative_path, commit_sha1 = nil, postfix: '') ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/cnvrg/files.rb', line 571

def download_file_s3(relative_path, commit_sha1=nil, postfix: '')
  begin
    res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {relative_path: relative_path,
                                                                        commit_sha1: commit_sha1,new_version:true})

    Cnvrg::CLI.is_response_success(res, false)
    if res["result"]
      download_resp = res
      filename = download_resp["result"]["filename"]
      sts_path = download_resp["result"]["path_sts"]
      retries = 0
      success= false
      while !success and retries < 20
        begin
          if !Helpers.is_verify_ssl
            body = open(sts_path, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE}).read
          else
            body = open(sts_path).read
          end
          success = true
        rescue => e
          retries +=1
          sleep(5)

        end
      end
      if !success
        puts "error in sts"
        return false
      end

      split = body.split("\n")
      key = split[0]
      iv = split[1]

      access =  Cnvrg::Helpers.decrypt(key, iv, download_resp["result"]["sts_a"])

      secret =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_s"])

      session =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["sts_st"])
      region =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["region"])

      bucket =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["bucket"])
      file_key =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])


      is_s3 = download_resp["result"]["is_s3"]
      if is_s3 or is_s3.nil?
        client = Aws::S3::Client.new(
            :access_key_id =>access,
            :secret_access_key => secret,
            :session_token => session,
            :region => region,
            :http_open_timeout => 60, :retry_limit => 20)
      else
        endpoint = Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["endpoint"])
        client = Aws::S3::Client.new(
            :access_key_id =>access,
            :secret_access_key => secret,
            :region => region,
            :endpoint=> endpoint,:force_path_style=> true,:ssl_verify_peer=>false,
            :http_open_timeout => 60, :retry_limit => 20)
      end
      absolute_path = "#{@project_home}/#{relative_path}#{postfix}"
      File.open(absolute_path, 'w+') do |file|
        resp = client.get_object({bucket:bucket,
                              key:file_key}, target: file)
      end
      return true
    end

  rescue => e
    puts "error in aws"

    puts e.message
      return false

  end
end