Get progress callback in aws boto3 uploads

荒凉一梦 提交于 2021-01-29 21:43:46

问题


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 test file'
k.set_contents_from_filename(testfile,
    cb=percent_cb, num_cb=10)

While I see the boto3 package takes a callback:

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

I don't see the equivalent of the num_cb argument. How can I get a progress meter for upload_fileobj using boto3?

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

回答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)


来源:https://stackoverflow.com/questions/56981929/get-progress-callback-in-aws-boto3-uploads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!