Skip to content

Commit aa37e30

Browse files
committed
change progress bar
1 parent c62fe9a commit aa37e30

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/common/s3util.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,22 @@ def file_exists_on_s3(self, key):
6060

6161
def put_file_obj(self, file_size, key, data, md5_base64):
6262
# Initialize the progress bar
63-
progress = create_progress_bar(file_size)
63+
# progress = create_progress_bar(file_size)
6464
chunk_size = 1024 * 1024 if file_size >= 1024 * 1024 else file_size #chunk data for display progress for small metadata file < 4,500,000,000 bytes
65-
try:
66-
# Upload the file in chunks
67-
for chunk in iter(lambda: data.read(chunk_size), b''):
68-
self.bucket.put_object(Key=key,
69-
Body=chunk,
70-
ContentMD5=md5_base64,
71-
ACL= BUCKET_OWNER_ACL,
72-
)
73-
# Update the progress bar
74-
progress.update(len(chunk))
75-
finally:
76-
# Close the progress bar
77-
progress.close()
65+
with ProgressPercentage(file_size) as progress:
66+
try:
67+
for chunk in iter(lambda: data.read(chunk_size), b''):
68+
self.bucket.put_object(
69+
Key=key,
70+
Body=chunk,
71+
ContentMD5=md5_base64,
72+
ACL=BUCKET_OWNER_ACL,
73+
)
74+
progress(len(chunk)) # Dynamically move progress bar
75+
76+
except Exception as e:
77+
print(f"[red]Upload failed: {e}[/red]")
78+
7879

7980
def upload_file_obj(self, file_size, key, data, config=None, extra_args={'ACL': BUCKET_OWNER_ACL}):
8081
self.bucket.upload_fileobj(
@@ -139,18 +140,36 @@ def close(self):
139140
"""
140141
S3 util class to display upload progress
141142
"""
142-
class ProgressPercentage(object):
143+
from rich.progress import Progress, BarColumn, TaskProgressColumn, TimeRemainingColumn, TransferSpeedColumn
144+
145+
class ProgressPercentage:
143146
def __init__(self, file_size):
144147
self._size = file_size
145148
self._seen_so_far = 0
146-
self._progress = create_progress_bar(file_size)
149+
self.progress = Progress(
150+
"{task.fields[status]}", # ✅ Dynamic status update
151+
BarColumn(),
152+
TaskProgressColumn(),
153+
TransferSpeedColumn(),
154+
"[yellow]{task.completed}B/{task.total}B",
155+
TimeRemainingColumn(),
156+
)
157+
self.task = None
158+
159+
def __enter__(self):
160+
self.progress.start()
161+
self.task = self.progress.add_task("[cyan]Uploading...[/cyan]", total=self._size, status="[cyan]Uploading...[/cyan]")
162+
return self
147163

148164
def __call__(self, bytes_amount):
149165
self._seen_so_far += bytes_amount
150-
self._progress.update(bytes_amount)
166+
self.progress.update(self.task, advance=bytes_amount)
167+
168+
def __exit__(self, exc_type, exc_value, traceback):
169+
# ✅ Change progress bar title to "Uploaded!" when done
170+
self.progress.update(self.task, completed=self._size, status="[green]Uploaded![/green]")
171+
self.progress.stop()
151172

152-
def __del__(self):
153-
self._progress.close()
154173

155174
def create_progress_bar(file_size):
156175
progress_bar = tqdm(total= file_size, unit='B', unit_scale=True, desc="Progress", smoothing=0.0,

0 commit comments

Comments
 (0)