Skip to content Skip to sidebar Skip to footer

Get Progress Callback In Aws Boto3 Uploads

There's a great question and answer for the original boto uploads here: How to upload a file to directory in S3 bucket using boto Which has a callback: k = Key(bucket) k.key = 'my

Solution 1:

If you don't need to limit the number of calling callback, (and there is no way to do it with upload_fileobj),
1. show percentage

import os
import boto3

class Test:
    def __init__(self):
        self.total = 0
        self.uploaded = 0
        self.s3 = boto3.client('s3')

    def upload_callback(self, size):
        if self.total == 0:
            return
        self.uploaded += size
        print("{} %".format(int(self.uploaded / self.total * 100)))

    def upload(self, bucket, key, file):
        self.total = os.stat(file).st_size

        with open(file, 'rb') as data:
            self.s3.upload_fileobj(
                data, bucket, key, Callback=self.upload_callback)
  1. using progressbar
import os
import boto3
import progressbar


class Test2:
    def __init__(self):
        self.s3 = boto3.client('s3')

    def upload_callback(self, size):
        self.pg.update(self.pg.currval + size)

    def upload(self, bucket, key, file):
        self.pg = progressbar.progressbar.ProgressBar(
            maxval=os.stat(file).st_size)
        self.pg.start()

        with open(file, 'rb') as data:
            self.s3.upload_fileobj(
                data, bucket, key, Callback=self.upload_callback)

Post a Comment for "Get Progress Callback In Aws Boto3 Uploads"