Skip to content

Commit

Permalink
Handle bucket not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ikornaselur committed Jan 28, 2020
1 parent 34677d9 commit 4d3b634
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions sucket/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from .utils import sizeof_fmt


class BucketException(Exception):
pass


class Bucket:
name: str
session: aiobotocore.AioSession
Expand All @@ -28,7 +32,11 @@ async def all_objects_paginator(self, prefix: str) -> AsyncIterator[List[Dict]]:
kwargs = {"Bucket": self.name, "Prefix": prefix}
async with self.session.create_client("s3") as client:
while True:
response = await client.list_objects_v2(**kwargs)
try:
response = await client.list_objects_v2(**kwargs)
except client.exceptions.NoSuchBucket as e:
raise BucketException("Bucket not found") from e

objects = response.get("Contents", [])
yield objects

Expand Down Expand Up @@ -58,8 +66,13 @@ def secho(self, msg: str, fg: str):
async def download_all_objects(self, prefix: str):
self.secho("[*] Fetching object metadata...", fg="green")
objects = []
async for page in self.all_objects_paginator(prefix):
objects.extend(page)
try:
async for page in self.all_objects_paginator(prefix):
objects.extend(page)
except BucketException as e:
self.secho(f"[-] {e}", fg="red")
return

total_size = sum(o["Size"] for o in objects)

if not objects:
Expand Down

0 comments on commit 4d3b634

Please sign in to comment.