File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ Download image files
1212
1313``` python
1414from PIL import Image
15- from fast_s3 import Fetcher
15+ from fast_s3 import Fetcher, Status
1616
1717
1818large_list_of_image_paths = [... ]
@@ -30,7 +30,8 @@ fetcher = Fetcher(
3030)
3131
3232for file in fetcher:
33- Image.open(file .buffer).save(file .path)
33+ if file .status != Status.error:
34+ Image.open(file .buffer).save(file .path)
3435
3536fetcher.close()
3637```
Original file line number Diff line number Diff line change 1+ from .file import File , Status
12from .fetcher import Fetcher
23from .uploader import Uploader
Original file line number Diff line number Diff line change 22from pathlib import Path
33from typing import List , Union
44
5- from .file import File
5+ from botocore .exceptions import ClientError
6+
7+ from .file import File , Status
68from .transfer_manager import transfer_manager
79
810
@@ -42,21 +44,24 @@ def __iter__(self):
4244
4345 if self .ordered :
4446 for _ in range (len (self )):
45- file = self .files .pop (0 )
46- file .future .result ()
47- yield file
48- self .queue_download_ ()
47+ yield self .process_index (0 )
4948 else :
5049 for _ in range (len (self )):
5150 for index , file in enumerate (self .files ):
5251 if file .future .done ():
5352 break
5453 else :
5554 index = 0
56- file = self .files .pop (index )
57- file .future .result ()
58- yield file
59- self .queue_download_ ()
55+ yield self .process_index (index )
56+
57+ def process_index (self , index ):
58+ file = self .files .pop (index )
59+ self .queue_download_ ()
60+ try :
61+ file .future .result ()
62+ return file .with_status (Status .done )
63+ except ClientError :
64+ return file .with_status (Status .error )
6065
6166 def queue_download_ (self ):
6267 if self .current_path_index < len (self ):
Original file line number Diff line number Diff line change 11import io
2+ from enum import Enum
23from pathlib import Path
34from typing import Union
45
56from pydantic import BaseModel
67from s3transfer .futures import TransferFuture
78
89
10+ class Status (str , Enum ):
11+ pending = "pending"
12+ done = "done"
13+ error = "error"
14+
15+
916class File (BaseModel ):
1017 buffer : io .BytesIO
1118 future : TransferFuture
1219 path : Union [str , Path ]
20+ status : Status = Status .pending
1321
1422 class Config :
1523 arbitrary_types_allowed = True
24+
25+ def with_status (self , status : Status ):
26+ attributes = self .dict ()
27+ attributes .update (status = status )
28+ return File (** attributes )
Original file line number Diff line number Diff line change 11[tool .poetry ]
22name = " fast_s3"
3- version = " 0.0.0 "
3+ version = " 0.0.2 "
44description = " Download images from s3 fast"
55authors = [" NextML AB" ]
66readme = " README.md"
You can’t perform that action at this time.
0 commit comments