@@ -226,6 +226,7 @@ def upload_blob(
226
226
container : container_type ,
227
227
layer : dict ,
228
228
do_chunked : bool = False ,
229
+ refresh_headers : bool = True ,
229
230
) -> requests .Response :
230
231
"""
231
232
Prepare and upload a blob.
@@ -239,6 +240,8 @@ def upload_blob(
239
240
:type container: oras.container.Container or str
240
241
:param layer: dict from oras.oci.NewLayer
241
242
:type layer: dict
243
+ :param refresh_headers: if true, headers are refreshed
244
+ :type refresh_headers: bool
242
245
"""
243
246
blob = os .path .abspath (blob )
244
247
container = self .get_container (container )
@@ -250,9 +253,13 @@ def upload_blob(
250
253
# This is currently disabled unless the user asks for it, as
251
254
# it doesn't seem to work for all registries
252
255
if not do_chunked :
253
- response = self .put_upload (blob , container , layer )
256
+ response = self .put_upload (
257
+ blob , container , layer , refresh_headers = refresh_headers
258
+ )
254
259
else :
255
- response = self .chunked_upload (blob , container , layer )
260
+ response = self .chunked_upload (
261
+ blob , container , layer , refresh_headers = refresh_headers
262
+ )
256
263
257
264
# If we have an empty layer digest and the registry didn't accept, just return dummy successful response
258
265
if (
@@ -474,7 +481,11 @@ def download_blob(
474
481
return outfile
475
482
476
483
def put_upload (
477
- self , blob : str , container : oras .container .Container , layer : dict
484
+ self ,
485
+ blob : str ,
486
+ container : oras .container .Container ,
487
+ layer : dict ,
488
+ refresh_headers : bool = True ,
478
489
) -> requests .Response :
479
490
"""
480
491
Upload to a registry via put.
@@ -485,9 +496,15 @@ def put_upload(
485
496
:type container: oras.container.Container or str
486
497
:param layer: dict from oras.oci.NewLayer
487
498
:type layer: dict
499
+ :param refresh_headers: if true, headers are refreshed
500
+ :type refresh_headers: bool
488
501
"""
489
502
# Start an upload session
490
503
headers = {"Content-Type" : "application/octet-stream" }
504
+
505
+ if not refresh_headers :
506
+ headers .update (self .headers )
507
+
491
508
upload_url = f"{ self .prefix } ://{ container .upload_blob_url ()} "
492
509
r = self .do_request (upload_url , "POST" , headers = headers )
493
510
@@ -541,7 +558,11 @@ def _get_location(
541
558
return session_url
542
559
543
560
def chunked_upload (
544
- self , blob : str , container : oras .container .Container , layer : dict
561
+ self ,
562
+ blob : str ,
563
+ container : oras .container .Container ,
564
+ layer : dict ,
565
+ refresh_headers : bool = True ,
545
566
) -> requests .Response :
546
567
"""
547
568
Upload via a chunked upload.
@@ -552,9 +573,14 @@ def chunked_upload(
552
573
:type container: oras.container.Container or str
553
574
:param layer: dict from oras.oci.NewLayer
554
575
:type layer: dict
576
+ :param refresh_headers: if true, headers are refreshed
577
+ :type refresh_headers: bool
555
578
"""
556
579
# Start an upload session
557
580
headers = {"Content-Type" : "application/octet-stream" , "Content-Length" : "0" }
581
+ if not refresh_headers :
582
+ headers .update (self .headers )
583
+
558
584
upload_url = f"{ self .prefix } ://{ container .upload_blob_url ()} "
559
585
r = self .do_request (upload_url , "POST" , headers = headers )
560
586
@@ -618,7 +644,10 @@ def _parse_response_errors(self, response: requests.Response):
618
644
pass
619
645
620
646
def upload_manifest (
621
- self , manifest : dict , container : oras .container .Container
647
+ self ,
648
+ manifest : dict ,
649
+ container : oras .container .Container ,
650
+ refresh_headers : bool = True ,
622
651
) -> requests .Response :
623
652
"""
624
653
Read a manifest file and upload it.
@@ -627,13 +656,19 @@ def upload_manifest(
627
656
:type manifest: dict
628
657
:param container: parsed container URI
629
658
:type container: oras.container.Container or str
659
+ :param refresh_headers: if true, headers are refreshed
660
+ :type refresh_headers: bool
630
661
"""
631
662
self .reset_basic_auth ()
632
663
jsonschema .validate (manifest , schema = oras .schemas .manifest )
633
664
headers = {
634
665
"Content-Type" : oras .defaults .default_manifest_media_type ,
635
666
"Content-Length" : str (len (manifest )),
636
667
}
668
+
669
+ if not refresh_headers :
670
+ headers .update (self .headers )
671
+
637
672
return self .do_request (
638
673
f"{ self .prefix } ://{ container .manifest_url ()} " , # noqa
639
674
"PUT" ,
@@ -659,6 +694,8 @@ def push(self, *args, **kwargs) -> requests.Response:
659
694
:type manifest_annotations: dict
660
695
:param target: target location to push to
661
696
:type target: str
697
+ :param refresh_headers: if true or None, headers are refreshed
698
+ :type refresh_headers: bool
662
699
:param subject: optional subject reference
663
700
:type subject: Subject
664
701
"""
@@ -675,6 +712,10 @@ def push(self, *args, **kwargs) -> requests.Response:
675
712
annotset = oras .oci .Annotations (kwargs .get ("annotation_file" ))
676
713
media_type = None
677
714
715
+ refresh_headers = kwargs .get ("refresh_headers" )
716
+ if refresh_headers is None :
717
+ refresh_headers = True
718
+
678
719
# Upload files as blobs
679
720
for blob in kwargs .get ("files" , []):
680
721
# You can provide a blob + content type
@@ -720,7 +761,9 @@ def push(self, *args, **kwargs) -> requests.Response:
720
761
logger .debug (f"Preparing layer { layer } " )
721
762
722
763
# Upload the blob layer
723
- response = self .upload_blob (blob , container , layer )
764
+ response = self .upload_blob (
765
+ blob , container , layer , refresh_headers = refresh_headers
766
+ )
724
767
self ._check_200_response (response )
725
768
726
769
# Do we need to cleanup a temporary targz?
@@ -762,13 +805,17 @@ def push(self, *args, **kwargs) -> requests.Response:
762
805
if config_file is None
763
806
else nullcontext (config_file )
764
807
) as config_file :
765
- response = self .upload_blob (config_file , container , conf )
808
+ response = self .upload_blob (
809
+ config_file , container , conf , refresh_headers = refresh_headers
810
+ )
766
811
767
812
self ._check_200_response (response )
768
813
769
814
# Final upload of the manifest
770
815
manifest ["config" ] = conf
771
- self ._check_200_response (self .upload_manifest (manifest , container ))
816
+ self ._check_200_response (
817
+ self .upload_manifest (manifest , container , refresh_headers = refresh_headers )
818
+ )
772
819
print (f"Successfully pushed { container } " )
773
820
return response
774
821
0 commit comments