【问题标题】:Get progress callback in aws boto3 uploads在 aws boto3 上传中获取进度回调
【发布时间】:2019-07-11 05:04:10
【问题描述】:

这里有一个很好的问题和答案,用于原始 boto 上传:

How to upload a file to directory in S3 bucket using boto

哪个有回调:

k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile,
    cb=percent_cb, num_cb=10)

虽然我看到 boto3 包需要回调:

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_fileobj

我没有看到 num_cb 参数的等效项。如何使用 boto3 获得 upload_fileobj 的进度表?

s3.upload_fileobj(data, 'mybucket', 'mykey')

【问题讨论】:

    标签: python-3.x amazon-web-services boto3


    【解决方案1】:

    如果不需要限制调用回调的次数,(而且upload_fileobj也没办法做到),
    1. 显示百分比

    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. 使用进度条
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多