I am in a situation with a document storage feature in a Django/Python application (using Boto3) where I also need to provide the user with information on the progress of an upload.
The methods I have seen which use straight JS are not all universally 'usable' across browsers, what I want is to be able upload a file using Boto3 and then also show an upload progress bar in my Django template.
Looking at the docs here: https://boto3.amazonaws.com/v1/documentation/api/latest/_modules/boto3/s3/transfer.html I am not understanding how it would be possible to show building progress of the upload calling ProgressPercentage()
from within a template.
How would I go about displaying the actual progress for my user in a template using Boto3?
class ProgressPercentage(object):
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify we'll assume this is hooked up
# to a single filename.
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write(
"\r%s %s / %s (%.2f%%)" % (
self._filename, self._seen_so_far, self._size,
percentage))
sys.stdout.flush()
transfer = S3Transfer(boto3.client('s3', 'us-west-2'))
# Upload /tmp/myfile to s3://bucket/key and print upload progress.
transfer.upload_file('/tmp/myfile', 'bucket', 'key',
callback=ProgressPercentage('/tmp/myfile'))