Skip to content

Commit 16c91bd

Browse files
author
FelixAbrahamsson
committed
improve: do not crash on failed downloads
1 parent 3538370 commit 16c91bd

5 files changed

Lines changed: 32 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Download image files
1212

1313
```python
1414
from PIL import Image
15-
from fast_s3 import Fetcher
15+
from fast_s3 import Fetcher, Status
1616

1717

1818
large_list_of_image_paths = [...]
@@ -30,7 +30,8 @@ fetcher = Fetcher(
3030
)
3131

3232
for 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

3536
fetcher.close()
3637
```

fast_s3/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
from .file import File, Status
12
from .fetcher import Fetcher
23
from .uploader import Uploader

fast_s3/fetcher.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from pathlib import Path
33
from typing import List, Union
44

5-
from .file import File
5+
from botocore.exceptions import ClientError
6+
7+
from .file import File, Status
68
from .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):

fast_s3/file.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
import io
2+
from enum import Enum
23
from pathlib import Path
34
from typing import Union
45

56
from pydantic import BaseModel
67
from s3transfer.futures import TransferFuture
78

89

10+
class Status(str, Enum):
11+
pending = "pending"
12+
done = "done"
13+
error = "error"
14+
15+
916
class 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)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fast_s3"
3-
version = "0.0.0"
3+
version = "0.0.2"
44
description = "Download images from s3 fast"
55
authors = ["NextML AB"]
66
readme = "README.md"

0 commit comments

Comments
 (0)