14
14
15
15
@dataclass
16
16
class UploadArtifactMeta :
17
- typegraph_name : str
17
+ typegraphName : str
18
18
hash : str
19
- size_in_bytes : int
20
- relative_path : str
19
+ sizeInBytes : int
20
+ relativePath : str
21
21
22
22
23
23
class ArtifactUploader :
@@ -49,10 +49,11 @@ def __fetch_upload_urls(
49
49
self ,
50
50
artifact_metas : List [UploadArtifactMeta ],
51
51
) -> List [str ]:
52
- artifacts_json = json .dumps (artifact_metas .__dict__ ).encode ()
52
+ artifacts_objs = [vars (meta ) for meta in artifact_metas ]
53
+ artifacts_json = json .dumps (artifacts_objs , indent = 4 ).encode ()
53
54
req = request .Request (
54
55
url = self .get_upload_url ,
55
- method = "PUT " ,
56
+ method = "POST " ,
56
57
headers = self .headers ,
57
58
data = artifacts_json ,
58
59
)
@@ -71,30 +72,33 @@ def __upload(
71
72
upload_headers ["Authorization" ] = self .auth .as_header_value ()
72
73
73
74
if url is None :
74
- print (f"Skipping upload for artifact: { meta .relative_path } " )
75
+ print (f"Skipping upload for artifact: { meta .relativePath } " )
75
76
return Ok (None )
76
77
77
- path = os .path .join (os .path .dirname (self .tg_path ), meta .relative_path )
78
+ path = os .path .join (os .path .dirname (self .tg_path ), meta .relativePath )
78
79
# TODO: read in chunks?
79
- with open (path , "r " ) as file :
80
+ with open (path , "rb " ) as file :
80
81
content = file .read ()
81
82
82
83
upload_req = request .Request (
83
84
url = url ,
84
85
method = "POST" ,
85
- data = content . encode () ,
86
+ data = content ,
86
87
headers = upload_headers ,
87
88
)
88
89
response = request .urlopen (upload_req )
89
- if response .status != 200 :
90
+ if response .status != 201 :
90
91
raise Exception (f"Failed to upload artifact { path } { response .status } " )
91
92
92
93
return handle_response (response .read ().decode ())
93
94
94
95
def get_metas (self , artifacts : List [Artifact ]) -> List [UploadArtifactMeta ]:
95
96
return [
96
97
UploadArtifactMeta (
97
- self .tg_name , artifact .hash , artifact .size , artifact .path
98
+ typegraphName = self .tg_name ,
99
+ hash = artifact .hash ,
100
+ sizeInBytes = artifact .size ,
101
+ relativePath = artifact .path ,
98
102
)
99
103
for artifact in artifacts
100
104
]
@@ -107,10 +111,10 @@ def __handle_errors(
107
111
errors = 0
108
112
for result , meta in zip (results , artifact_metas ):
109
113
if isinstance (result , Err ):
110
- print (f"Failed to upload artifact { meta .relative_path } : { result .value } " )
114
+ print (f"Failed to upload artifact { meta .relativePath } : { result .value } " )
111
115
errors += 1
112
116
else :
113
- print (f"Successfuly uploaded artifact { meta .relative_path } " )
117
+ print (f"Successfuly uploaded artifact { meta .relativePath } " )
114
118
115
119
if errors > 0 :
116
120
raise Exception (f"Failed to upload { errors } artifacts" )
0 commit comments