Created
August 30, 2018 02:31
-
-
Save emmanuelnk/bcf6548eb02c077dc9e5f43be8bd11dc to your computer and use it in GitHub Desktop.
Download file from FTP Server, upload file to S3 with Progress Bar in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import progressbar | |
from hurry.filesize import size | |
filesize = ftp.size(file) | |
try: | |
down_progress = progressbar.AnimatedProgressBar(end=filesize, width=50) | |
with open(TEMP_DIR + file, 'wb') as f: | |
def download_progress(chunk): | |
f.write(chunk) | |
down_progress + len(chunk) | |
down_progress.show_progress() | |
print('downloading: {} | size: {}'.format(file, size(filesize))) | |
ftp.retrbinary('RETR {}'.format(file), download_progress) | |
print('\n{} successfully downloaded'.format(file)) | |
except Exception as exc: | |
print('Error downloading file {}!'.format(file), exc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
class ProgressBar(object): | |
"""ProgressBar class holds the options of the progress bar. | |
The options are: | |
start State from which start the progress. For example, if start is | |
5 and the end is 10, the progress of this state is 50% | |
end State in which the progress has terminated. | |
width -- | |
fill String to use for "filled" used to represent the progress | |
blank String to use for "filled" used to represent remaining space. | |
format Format | |
incremental | |
""" | |
def __init__(self, start=0, end=10, width=12, fill='=', blank='.', format='[%(fill)s>%(blank)s] %(progress)s%%', incremental=True): | |
super(ProgressBar, self).__init__() | |
self.start = start | |
self.end = end | |
self.width = width | |
self.fill = fill | |
self.blank = blank | |
self.format = format | |
self.incremental = incremental | |
self.step = 100 / float(width) #fix | |
self.reset() | |
def __add__(self, increment): | |
increment = self._get_progress(increment) | |
if 100 > self.progress + increment: | |
self.progress += increment | |
else: | |
self.progress = 100 | |
return self | |
def __str__(self): | |
progressed = int(self.progress / self.step) #fix | |
fill = progressed * self.fill | |
blank = (self.width - progressed) * self.blank | |
return self.format % {'fill': fill, 'blank': blank, 'progress': int(self.progress)} | |
__repr__ = __str__ | |
def _get_progress(self, increment): | |
return float(increment * 100) / self.end | |
def reset(self): | |
"""Resets the current progress to the start point""" | |
self.progress = self._get_progress(self.start) | |
return self | |
class AnimatedProgressBar(ProgressBar): | |
"""Extends ProgressBar to allow you to use it straighforward on a script. | |
Accepts an extra keyword argument named `stdout` (by default use sys.stdout) | |
and may be any file-object to which send the progress status. | |
""" | |
def __init__(self, *args, **kwargs): | |
super(AnimatedProgressBar, self).__init__(*args, **kwargs) | |
self.stdout = kwargs.get('stdout', sys.stdout) | |
def show_progress(self): | |
if hasattr(self.stdout, 'isatty') and self.stdout.isatty(): | |
self.stdout.write('\r') | |
else: | |
self.stdout.write('\n') | |
self.stdout.write(str(self)) | |
self.stdout.flush() | |
if __name__ == '__main__': | |
p = AnimatedProgressBar(end=100, width=80) | |
while True: | |
p + 5 | |
p.show_progress() | |
time.sleep(0.1) | |
if p.progress == 100: | |
break | |
print #new line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import progressbar | |
import boto3 | |
from hurry.filesize import size | |
bucket = BUCKET_NAME | |
s3_client = boto3.resource('s3') | |
s3_dir ="some_type_of_file/" | |
filesize = ftp.size(file) | |
try: | |
# upload file to s3 | |
print("\nuploading {} | size: {}".format(file, size(filesize))) | |
up_progress = progressbar.AnimatedProgressBar(end=filesize, width=50) | |
def upload_progress(chunk): | |
up_progress + chunk # Notice! No len() | |
up_progress.show_progress() | |
s3_client.meta.client.upload_file(file, bucket, s3_path, Callback=upload_progress) | |
# s3_client.meta.client.upload_file(file, bucket, s3_path) # dont show progress | |
print("\nFile {} uploaded to S3 bucket:{} path:{}".format(file, bucket, s3_dir)) | |
except: | |
print("Error uploading file {} to s3!".format(file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment