Method: Cnvrg::Files#show_file_s3

Defined in:
lib/cnvrg/files.rb

#show_file_s3(relative_path, commit_sha1 = nil) ⇒ Object



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
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
# File 'lib/cnvrg/files.rb', line 550

def show_file_s3(relative_path, commit_sha1=nil)
  begin
    res = Cnvrg::API.request(@base_resource + "download_file", 'POST', { absolute_path: '', 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"]

      #absolute_path += ".conflict" if conflict
      sts_path = download_resp["result"]["path_sts"]
      uri = URI.parse(sts_path)
      http_object = Net::HTTP.new(uri.host, uri.port)
      http_object.use_ssl = true if uri.scheme == 'https'
      request = Net::HTTP::Get.new(sts_path)

      body = ""
      http_object.start do |http|
        response = http.request request
        body = response.read_body
      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"])
      key =  Cnvrg::Helpers.decrypt(key,iv, download_resp["result"]["key"])

      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
      )
      resp = client.get_object({bucket:bucket,
                              key:key})
      return resp.body.string
    end

  rescue => e
    puts e
    return false

  end
end