Class: Cnvrg::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/files.rb

Constant Summary collapse

LARGE_FILE =
1024*1024*5
MULTIPART_SPLIT =
10000000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, project_slug) ⇒ Files

Returns a new instance of Files.



15
16
17
18
19
# File 'lib/cnvrg/files.rb', line 15

def initialize(owner, project_slug)
  @project_slug = project_slug
  @owner = owner
  @base_resource = "users/#{owner}/projects/#{project_slug}/"
end

Instance Attribute Details

#base_resourceObject (readonly)

Returns the value of attribute base_resource.



13
14
15
# File 'lib/cnvrg/files.rb', line 13

def base_resource
  @base_resource
end

Instance Method Details

#create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object



372
373
374
375
# File 'lib/cnvrg/files.rb', line 372

def create_dir(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#delete_commit_files_local(deleted) ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
# File 'lib/cnvrg/files.rb', line 645

def delete_commit_files_local(deleted)
  begin
    FileUtils.rm_rf(deleted) unless (deleted.nil? or deleted.empty?)
    return true
  rescue => e
    return false
  end

  return true

end

#delete_dir(absolute_path, relative_path, commit_sha1) ⇒ Object



367
368
369
370
# File 'lib/cnvrg/files.rb', line 367

def delete_dir(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#delete_file(absolute_path, relative_path, commit_sha1) ⇒ Object



362
363
364
365
# File 'lib/cnvrg/files.rb', line 362

def delete_file(absolute_path, relative_path, commit_sha1)
  response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1})
  return Cnvrg::CLI.is_response_success(response, false)
end

#download_cnvrg_image(image_name, secret) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cnvrg/files.rb', line 205

def download_cnvrg_image(image_name, secret)
  res =Cnvrg::API.request("images/#{image_name}/" + "download", 'POST', {secret:secret},true)
  Cnvrg::CLI.is_response_success(res, true)
  if res["result"]
    download_resp = res
    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
    )

    File.open("/tmp/#{image_name}.tar", 'w+') do |file|
      resp = client.get_object({bucket:bucket,
                                key:key}, target: file)
    end
    return true
  end

rescue => e
  puts e
  return false


end

#download_dir(absolute_path, relative_path, project_home) ⇒ Object



604
605
606
# File 'lib/cnvrg/files.rb', line 604

def download_dir(absolute_path, relative_path, project_home)
  FileUtils.mkdir_p("#{project_home}/#{absolute_path}")
end

#download_file(absolute_path, relative_path, project_home, conflict = false) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/cnvrg/files.rb', line 529

def download_file(absolute_path, relative_path, project_home, conflict=false)
  res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path})
  Cnvrg::CLI.is_response_success(res, false)
  if res["result"]
    res = res["result"]
    return false if res["link"].empty? or res["filename"].empty?
    filename = res["filename"]
    file_location = absolute_path.gsub(/#{filename}\/?$/, "")

    FileUtils.mkdir_p project_home + "/" + file_location
    filename += ".conflict" if conflict

    File.open("#{project_home}/#{file_location}/#{filename}", "wb") do |file|
      file.write open(res["link"]).read
    end
  else
    return false
  end
  return true
end

#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

#download_image(file_path_to_store, image_slug, owner) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cnvrg/files.rb', line 181

def download_image(file_path_to_store, image_slug, owner)


  download_resp = Cnvrg::API.request("users/#{owner}/images/#{image_slug}/" + "download", 'GET')
  path = download_resp["result"]["path"]

  if Cnvrg::CLI.is_response_success(download_resp, false)
    begin
      open(file_path_to_store, 'wb') do |file|
        file << open(path).read
      end

      return true
    rescue => e
      return false
    end

    return true
  else
    return false
  end


end

#download_multpile_files_s3(files, project_home) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/cnvrg/files.rb', line 458

def download_multpile_files_s3(files, project_home)
  begin
      props = Cnvrg::Helpers.get_s3_props(files)
      client = props[:client]
      iv = props[:iv]
      key = props[:key]
      bucket = props[:bucket]
      download_succ_count = 0
      parallel_options = {
          :progress => {
              :title => "Download Progress",
              :progress_mark => '=',
              :format => "%b>>%i| %p%% %t",
              :starting_at => 0,
              :total => files["keys"].size,
              :autofinish => true
          },
          in_threads: Cnvrg::Helpers.parallel_threads,
          isolation: true
      }
      Parallel.map(files["keys"], parallel_options) do |f|

        file_path = f["name"]
        if file_path.end_with? "/"
          # dir
          if download_dir(file_path, file_path, project_home)
            download_succ_count += 1
          else
            return Cnvrg::Result.new(false,"Could not create directory: #{file_path}")
            raise Parallel::Kill
          end
        else
          # blob
          begin
          file_key =  Cnvrg::Helpers.decrypt(key,iv, f["path"])
          resp = false
          File.open(project_home+"/"+file_path, 'w+') do |file|
            resp = client.get_object({bucket:bucket,
                                      key:file_key}, target: file)
            #TODO update idx here!
          end
          if resp
            download_succ_count +=1
          else
            return Cnvrg::Result.new(false,"Could not create file: #{file_path}")
          end



          rescue => e
            return Cnvrg::Result.new(false,"Could not create file: #{file_path}", e.message, e.backtrace)
            raise Parallel::Kill
          end



          end
      end
    if download_succ_count == files["keys"].size
      return Cnvrg::Result.new(true,"Done.\nDownloaded #{download_succ_count} files")
    end
  rescue => e

    return Cnvrg::Result.new(false,"Could not download some files", e.message, e.backtrace)
  end




end

#end_commit(commit_sha1, force: false, message: "") ⇒ Object



663
664
665
666
# File 'lib/cnvrg/files.rb', line 663

def end_commit(commit_sha1,force:false,message:"")
  response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1,force:force,message:message})
  return response
end

#revoke_clone(project_home) ⇒ Object



638
639
640
641
642
643
644
# File 'lib/cnvrg/files.rb', line 638

def revoke_clone(project_home)
  begin
    FileUtils.rm_rf(project_home)
  rescue
  end

end

#revoke_download(conflicted_changes, downloaded_changes) ⇒ Object



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/cnvrg/files.rb', line 623

def revoke_download(conflicted_changes,downloaded_changes)
  begin
    if !conflicted_changes.nil? and !conflicted_changes.empty?
      conflicted_changes.each do |c|
        # FileUtils.rm_rf(c+".conflict")
      end
    end
    # FileUtils.rm_rf(downloaded_changes) unless (downloaded_changes.nil? or downloaded_changes.empty?)
  rescue => e
    return false
  end

  return true

end

#revoke_download_dir(absolute_path, relative_path, project_home) ⇒ Object



608
609
610
# File 'lib/cnvrg/files.rb', line 608

def revoke_download_dir(absolute_path, relative_path, project_home)
  puts FileUtils.rmtree("#{absolute_path}")
end

#revoke_download_file(project_home, absolute_path, filename, conflict = false) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
# File 'lib/cnvrg/files.rb', line 612

def revoke_download_file(project_home, absolute_path, filename, conflict=false)
  begin
    file_location = absolute_path.gsub(/#{filename}\/?$/, "")

    filename += ".conflict" if conflict
    FileUtils.remove("#{file_location}/#{filename}")
    return true
  rescue
    return false
  end
end

#rollback_commit(commit_sha1) ⇒ Object



668
669
670
671
# File 'lib/cnvrg/files.rb', line 668

def rollback_commit(commit_sha1)
  response = Cnvrg::API.request("#{base_resource}/commit/rollback", 'POST', {commit_sha1: commit_sha1})
  Cnvrg::CLI.is_response_success(response, false)
end

#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

#start_commit(new_branch, force: false, exp_start_commit: nil, job_slug: nil, job_type: nil) ⇒ Object



656
657
658
659
660
661
# File 'lib/cnvrg/files.rb', line 656

def start_commit(new_branch,force:false, exp_start_commit:nil, job_slug: nil, job_type: nil)
  response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {project_slug: @project_slug, new_branch: new_branch,force:force,
                                                                          username: @owner, exp_start_commit:exp_start_commit, job_slug: job_slug, job_type: job_type})
  Cnvrg::CLI.is_response_success(response,false)
  return response
end

#upload_cnvrg_image(absolute_path, image_name, secret) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cnvrg/files.rb', line 151

def upload_cnvrg_image(absolute_path, image_name,secret)
  file_name = File.basename absolute_path
  file_size = File.size(absolute_path).to_f
  content_type = MimeMagic.by_path(absolute_path)
  begin
    upload_resp = Cnvrg::API.request("images/#{image_name}/upload", 'POST_FILE', {relative_path: absolute_path,
                                                                                              file_name: file_name,
                                                                                              file_size: file_size,
                                                                                              file_content_type: content_type,
                                                                                              secret:secret
                                                                                              })
    # puts upload_resp
    if Cnvrg::CLI.is_response_success(upload_resp, false)
      path = upload_resp["result"]["path"]
      s3_res = upload_large_files_s3(upload_resp, absolute_path)
      if s3_res
          return true
      else
        return false
      end

    end
  rescue => e
    return false
  end
    return false


end

#upload_exec_file(absolute_path, image_name, commit_id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cnvrg/files.rb', line 73

def upload_exec_file(absolute_path, image_name, commit_id)
  file_name = File.basename absolute_path
  file_size = File.size(absolute_path).to_f
  content_type = "application/zip"
  begin
    upload_resp = Cnvrg::API.request("users/#{@owner}/images/" + "upload_config", 'POST_FILE', {relative_path: absolute_path,
                                                                                                file_name: file_name,
                                                                                                image_name: image_name,
                                                                                                file_size: file_size,
                                                                                                file_content_type: content_type,
                                                                                                project_slug: @project_slug,
                                                                                                commit_id: commit_id})
    # puts upload_resp
    if Cnvrg::CLI.is_response_success(upload_resp, false)
      if upload_resp["result"]["image"] == -1
        return -1
      end
      path = upload_resp["result"]["path"]
      s3_res = upload_small_files_s3(path, absolute_path, content_type)

    end
    if s3_res
      return upload_resp["result"]["id"]
    end
    return false
  rescue SignalException

    say "\nAborting"
    exit(1)
  end

end

#upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object



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
46
47
48
49
50
51
52
53
# File 'lib/cnvrg/files.rb', line 21

def upload_file(absolute_path, relative_path, commit_sha1)
  file_name = File.basename relative_path
  file_size = File.size(absolute_path).to_f
  mime_type = MimeMagic.by_path(absolute_path)
  content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain"
  sha1 =  OpenSSL::Digest::SHA1.file(absolute_path).hexdigest
  upload_resp = Cnvrg::API.request(@base_resource + "upload_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
                                                                                 commit_sha1: commit_sha1, file_name: file_name,
                                                                                 file_size: file_size, file_content_type: content_type, sha1: sha1,
                                                                                  new_version:true,only_large:true})


  if Cnvrg::CLI.is_response_success(upload_resp, false)

    path = upload_resp["result"]["path"]
    s3_res = upload_large_files_s3(upload_resp, absolute_path)

    # if file_size.to_f>= Cnvrg::Files::LARGE_FILE.to_f
    #   s3_res = upload_large_files_s3(upload_resp, absolute_path)
    # else
    #   s3_res = upload_small_files_s3(path, absolute_path, content_type)
    # end
    return s3_res
    # if s3_res
    # update_s3_resp = Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"],
    #                                                           blob_id: upload_resp["result"]["id"]})
    # is_suc = Cnvrg::CLI.is_response_success(update_s3_resp, false)
    #
    # return is_suc
  end
  return false

end

#upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message, commit_id) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cnvrg/files.rb', line 107

def upload_image(absolute_path, image_name, owner, is_public, is_base, dpkg, libraries, bash, message, commit_id)
  file_name = File.basename absolute_path
  file_size = File.size(absolute_path).to_f
  if is_base

    content_type = "application/zip"
  else
    content_type = "application/gzip"
  end
  begin
    upload_resp = Cnvrg::API.request("users/#{owner}/images/" + "upload_cnvrg", 'POST_FILE', {relative_path: absolute_path,
                                                                                              file_name: file_name,
                                                                                              image_name: image_name,
                                                                                              file_size: file_size,
                                                                                              file_content_type: content_type,
                                                                                              is_public: is_public,
                                                                                              project_slug: @project_slug,
                                                                                              commit_id: commit_id,
                                                                                              dpkg: dpkg,
                                                                                              py2: libraries,
                                                                                              py3: libraries,

                                                                                              bash_history: bash,
                                                                                              commit_message: message,
                                                                                              is_base: is_base})
    # puts upload_resp
    if Cnvrg::CLI.is_response_success(upload_resp, false)
      s3_res = upload_large_files_s3(upload_resp, absolute_path)
      if s3_res
        commit_resp = Cnvrg::API.request("users/#{owner}/images/#{upload_resp["result"]["id"]}/" + "commit", 'GET')
        if Cnvrg::CLI.is_response_success(commit_resp, false)
          return commit_resp["result"]["image"]
        else
          return false
        end

      end
    end
    return false
  rescue => e
  end

end

#upload_large_files_s3(upload_resp, file_path) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/cnvrg/files.rb', line 256

def upload_large_files_s3(upload_resp, file_path)
  begin
      sts_path = upload_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
        return false
      end
      split = body.split("\n")
      key = split[0]
      iv = split[1]

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

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

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

      bucket =  Cnvrg::Helpers.decrypt(key,iv, upload_resp["result"]["bucket"])
      is_s3 = upload_resp["result"]["is_s3"]
      if is_s3 or is_s3.nil?
        use_accelerate_endpoint =true
      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, upload_resp["result"]["endpoint"])
        use_accelerate_endpoint = false
        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

        s3 = Aws::S3::Resource.new(client: client)
        resp = s3.bucket(bucket).
            object(upload_resp["result"]["path"]+"/"+File.basename(file_path)).
            upload_file(file_path, {:use_accelerate_endpoint => use_accelerate_endpoint, :server_side_encryption => 'AES256'})

      return resp

  rescue => e
    puts e
    return false
  rescue SignalException
    return false

  end
    return true

end

#upload_log_file(absolute_path, relative_path, log_date) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cnvrg/files.rb', line 55

def upload_log_file(absolute_path, relative_path, log_date)
  file_name = File.basename relative_path
  file_size = File.size(absolute_path).to_f
  content_type = "text/x-log"
  upload_resp = Cnvrg::API.request("/users/#{@owner}/" + "upload_cli_log", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path,
                                                                                         file_name: file_name, log_date: log_date,
                                                                                         file_size: file_size, file_content_type: content_type})
  if Cnvrg::CLI.is_response_success(upload_resp, false)
   s3_res = upload_large_files_s3(upload_resp, absolute_path)

  end
  if s3_res
    return true
  end
  return false

end

#upload_small_files_s3(url_path, file_path, content_type) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/cnvrg/files.rb', line 330

def upload_small_files_s3(url_path, file_path, content_type)
  url = URI.parse(url_path)
  file = File.open(file_path, "rb")
  body = file.read
  begin
    Net::HTTP.start(url.host) do |http|
      if !Helpers.is_verify_ssl
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
      http.send_request("PUT", url.request_uri, body, {
          "content-type" => content_type,
      })
    end
    return true
  rescue Interrupt
    return false
  rescue => e
    puts e
    return false
  end
end

#upload_url(file_path) ⇒ Object



352
353
354
355
356
357
358
359
360
# File 'lib/cnvrg/files.rb', line 352

def upload_url(file_path)
  response = Cnvrg::API.request(@base_resource + "upload_url", 'POST', {file_s3_path: file_path})
  if Cnvrg::CLI.is_response_success(response, false)
    return response
  else
    return nil
  end

end