2525 FileUploadData ,
2626 UploadedPart ,
2727)
28- from tempfile import NamedTemporaryFile
2928from urllib .parse import urljoin
29+ import aiofiles
3030from ._utils import (
3131 DEFAULT_TIMEOUT_SECONDS ,
3232 PaginationGenerator ,
@@ -92,23 +92,26 @@ async def download_file_async(
9292 raise RuntimeError (
9393 f"destination_folder: { destination_folder } must be a directory"
9494 )
95- downloaded_file = Path (
96- NamedTemporaryFile (dir = destination_folder , delete = False ).name
97- )
98- async with AsyncHttpClient (
99- configuration = self .api_client .configuration , timeout = timeout_seconds
100- ) as session :
101- url = urljoin (
102- self .api_client .configuration .host , f"/v0/files/{ file_id } /content"
103- )
104- async for response in await session .stream (
105- "GET" , url = url , auth = self ._auth , follow_redirects = True
106- ):
107- response .raise_for_status ()
108- with open (downloaded_file , mode = "wb" ) as f :
95+ async with aiofiles .tempfile .NamedTemporaryFile (
96+ mode = "wb" ,
97+ dir = f"{ destination_folder .resolve ()} "
98+ if destination_folder is not None
99+ else None ,
100+ delete = False ,
101+ ) as downloaded_file :
102+ async with AsyncHttpClient (
103+ configuration = self .api_client .configuration , timeout = timeout_seconds
104+ ) as session :
105+ url = urljoin (
106+ self .api_client .configuration .host , f"/v0/files/{ file_id } /content"
107+ )
108+ async for response in await session .stream (
109+ "GET" , url = url , auth = self ._auth , follow_redirects = True
110+ ):
111+ response .raise_for_status ()
109112 async for chunk in response .aiter_bytes ():
110- f .write (chunk )
111- return str ( downloaded_file .resolve ())
113+ await downloaded_file .write (chunk )
114+ return f" { downloaded_file .name } "
112115
113116 def upload_file (
114117 self ,
@@ -130,7 +133,7 @@ async def upload_file_async(
130133 file = Path (file )
131134 if not file .is_file ():
132135 raise RuntimeError (f"{ file } is not a file" )
133- checksum : str = compute_sha256 (file )
136+ checksum : str = await compute_sha256 (file )
134137 for file_result in self ._search_files (
135138 sha256_checksum = checksum , timeout_seconds = timeout_seconds
136139 ):
0 commit comments