logi c в этом случае предназначен для создания QObject, который имеет сигнал, указывающий на прогресс, кроме того, задача загрузки должна выполняться во вторичном потоке, чтобы GUI не зависал:
import math
import os
import sys
import threading
import boto3
from PyQt5 import QtCore, QtWidgets
class S3Worker(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
percentageChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self._s3 = boto3.client("s3")
@property
def s3(self):
return self._s3
def upload(self, filename, bucketname, objectname):
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
threading.Thread(
target=self._execute, args=(filename, bucketname, objectname), daemon=True
).start()
def _execute(self, fileName, bucketName, objectName):
self.started.emit()
self.s3.upload_file(fileName, bucketName, objectName, Callback=self._callback)
self.finished.emit()
def _callback(self, bytes_amount):
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
self.percentageChanged.emit(math.floor(percentage))
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.filename_le = QtWidgets.QLineEdit()
self.upload_btn = QtWidgets.QPushButton("Upload")
self.percentage_pb = QtWidgets.QProgressBar()
lay = QtWidgets.QGridLayout(self)
lay.addWidget(QtWidgets.QLabel("filename:"))
lay.addWidget(self.filename_le, 0, 1)
lay.addWidget(self.upload_btn, 0, 2)
lay.addWidget(self.percentage_pb, 1, 0, 1, 3)
self.qs3 = S3Worker()
self.upload_btn.clicked.connect(self.start_upload)
self.qs3.started.connect(lambda: self.upload_btn.setEnabled(False))
self.qs3.finished.connect(lambda: self.upload_btn.setEnabled(True))
self.qs3.percentageChanged.connect(self.percentage_pb.setValue)
def start_upload(self):
filename = self.filename_le.text()
if os.path.exists(filename):
self.qs3.upload(filename, "mybucket", "foobject")
def main():
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()