Method: Cnvrg::Files#download_file_s3

Defined in:
lib/cnvrg/files.rb

#download_file_s3(absolute_path, relative_path, project_home, commit_sha1 = nil, conflict = false) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/cnvrg/files.rb', line 378

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

      File.open(project_home+"/"+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