Skip to content

Instantly share code, notes, and snippets.

@egeulgen
Last active October 28, 2024 12:22
Show Gist options
  • Save egeulgen/538aadc90275d79d514a5bacc4d5694e to your computer and use it in GitHub Desktop.
Save egeulgen/538aadc90275d79d514a5bacc4d5694e to your computer and use it in GitHub Desktop.
To display progress bar and percentage when downloading with boto3
class ProgressPercentage(object):
''' Progress Class
Class for calculating and displaying download progress
'''
def __init__(self, client, bucket, filename):
''' Initialize
initialize with: file name, file size and lock.
Set seen_so_far to 0. Set progress bar length
'''
self._filename = filename
self._size = client.head_object(Bucket=bucket, Key=filename)['ContentLength']
self._seen_so_far = 0
self._lock = threading.Lock()
self.prog_bar_len = 80
def __call__(self, bytes_amount):
''' Call
When called, increments seen_so_far by bytes_amount,
calculates percentage of seen_so_far/total file size
and prints progress bar.
'''
# To simplify we'll assume this is hooked up to a single filename.
with self._lock:
self._seen_so_far += bytes_amount
ratio = round((float(self._seen_so_far) / float(self._size)) * (self.prog_bar_len - 6), 1)
current_length = int(round(ratio))
percentage = round(100 * ratio / (self.prog_bar_len - 6), 1)
bars = '+' * current_length
output = bars + ' ' * (self.prog_bar_len - current_length - len(str(percentage)) - 1) + str(percentage) + '%'
if self._seen_so_far != self._size:
sys.stdout.write(output + '\r')
else:
sys.stdout.write(output + '\n')
sys.stdout.flush()
## Example usage
import boto3
s3 = boto3.resource('s3')
progress = ProgressPercentage(client, BUCKET_NAME, FILE_NAME)
s3.Bucket(BUCKET_NAME).download_file(FILE_NAME, DEST_NAME, Callback=progress)
@rdemorais
Copy link

thank you for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment