6
6
import urllib
7
7
import urllib .parse
8
8
import warnings
9
- from typing import Any , Callable , Dict , List , Optional , Union
9
+ from typing import Any , Callable , Dict , List , Optional
10
10
11
11
import requests
12
12
@@ -241,14 +241,26 @@ def upload_file_to_s3(self, project_id: str, file_path: str, content_type: Optio
241
241
content_type: アップロードするファイルのMIME Type. Noneの場合、ファイルパスから推測する。
242
242
243
243
Returns:
244
- AnnoFabに登録するときのpath
244
+ 一時データ保存先であるS3パス
245
245
"""
246
246
247
247
# content_type を推測
248
248
new_content_type = self ._get_content_type (file_path , content_type )
249
- return self ._upload_file_to_s3 (project_id , file_path , content_type = new_content_type )
249
+ with open (file_path , 'rb' ) as f :
250
+ return self .upload_file_object_to_s3 (project_id , fp = f , content_type = new_content_type )
251
+
252
+ def upload_file_object_to_s3 (self , project_id : str , fp : typing .IO , content_type : str ) -> str :
253
+ """
254
+ createTempPath APIを使ってアップロード用のURLとS3パスを取得して、"file object"をアップロードする。
255
+
256
+ Args:
257
+ project_id: プロジェクトID
258
+ fp: アップロードするファイルのfile object
259
+ content_type: アップロードするfile objectのMIME Type.
250
260
251
- def _upload_file_to_s3 (self , project_id : str , fp : Union [str , typing .IO ], content_type : str ) -> str :
261
+ Returns:
262
+ 一時データ保存先であるS3パス
263
+ """
252
264
# 一時データ保存先を取得
253
265
content = self .api .create_temp_path (project_id , header_params = {'content-type' : content_type })[0 ]
254
266
@@ -259,12 +271,7 @@ def _upload_file_to_s3(self, project_id: str, fp: Union[str, typing.IO], content
259
271
s3_url = content ["url" ].split ("?" )[0 ]
260
272
261
273
# アップロード
262
- if isinstance (fp , str ):
263
- with open (fp , 'rb' ) as f :
264
- res_put = self .api .session .put (s3_url , params = query_dict , data = f ,
265
- headers = {'content-type' : content_type })
266
- else :
267
- res_put = self .api .session .put (s3_url , params = query_dict , data = fp , headers = {'content-type' : content_type })
274
+ res_put = self .api .session .put (s3_url , params = query_dict , data = fp , headers = {'content-type' : content_type })
268
275
269
276
annofabapi .utils .log_error_response (logger , res_put )
270
277
annofabapi .utils .raise_for_status (res_put )
@@ -818,7 +825,7 @@ def get_latest_instruction(self, project_id: str) -> Optional[Instruction]:
818
825
def upload_instruction_image (self , project_id : str , image_id : str , file_path : str ,
819
826
content_type : Optional [str ] = None ) -> str :
820
827
"""
821
- 作業ガイドの画像をアップロードする。image_idはUUIDv4
828
+ 作業ガイドの画像をアップロードする。
822
829
823
830
Args:
824
831
project_id: プロジェクトID
@@ -827,15 +834,30 @@ def upload_instruction_image(self, project_id: str, image_id: str, file_path: st
827
834
content_type: アップロードするファイルのMIME Type. Noneの場合、ファイルパスから推測する。
828
835
829
836
Returns:
830
- AnnoFabに登録するときのpath
837
+ 一時データ保存先であるS3パス
831
838
"""
832
-
833
- # content_type を推測
834
839
new_content_type = self ._get_content_type (file_path , content_type )
840
+ with open (file_path , 'rb' ) as f :
841
+ return self .upload_file_object_as_instruction_image (project_id , image_id , fp = f ,
842
+ content_type = new_content_type )
843
+
844
+ def upload_file_object_as_instruction_image (self , project_id : str , image_id : str , fp : typing .IO ,
845
+ content_type : str ) -> str :
846
+ """
847
+ file objectを作業ガイドの画像としてアップロードする。
848
+
849
+ Args:
850
+ project_id: プロジェクトID
851
+ image_id: 作業ガイド画像ID
852
+ fp: アップロードするファイルのfile object
853
+ content_type: アップロードするファイルのMIME Type.
835
854
855
+ Returns:
856
+ 一時データ保存先であるS3パス
857
+ """
836
858
# 作業ガイド登録用/更新用のURLを取得
837
859
content = self .api .get_instruction_image_url_for_put (project_id , image_id ,
838
- header_params = {'content-type' : new_content_type })[0 ]
860
+ header_params = {'content-type' : content_type })[0 ]
839
861
840
862
url_parse_result = urllib .parse .urlparse (content ["url" ])
841
863
query_dict = urllib .parse .parse_qs (url_parse_result .query )
@@ -844,9 +866,7 @@ def upload_instruction_image(self, project_id: str, image_id: str, file_path: st
844
866
s3_url = content ["url" ].split ("?" )[0 ]
845
867
846
868
# アップロード
847
- with open (file_path , 'rb' ) as f :
848
- res_put = self .api .session .put (s3_url , params = query_dict , data = f ,
849
- headers = {'content-type' : new_content_type })
869
+ res_put = self .api .session .put (s3_url , params = query_dict , data = fp , headers = {'content-type' : content_type })
850
870
annofabapi .utils .log_error_response (logger , res_put )
851
871
annofabapi .utils .raise_for_status (res_put )
852
872
return content ["path" ]
0 commit comments