Boto3回调,显示上传进度

时间:2019-07-16 21:41:48

标签: python django python-3.x amazon-s3 boto3

我处于使用Django / Python应用程序(使用Boto3)的文档存储功能中,我还需要向用户提供有关上传进度的信息。

我看到的使用直接JS的方法并非在浏览器中普遍都“可用”,我想要的是能够使用Boto3上传文件,然后在Django模板中显示上传进度栏。

在这里查看文档:{​​{3}}我不理解如何从模板中显示上载调用ProgressPercentage()的构建进度。

如何使用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'))

1 个答案:

答案 0 :(得分:0)

SO中有三个相关的答案:

  1. Get progress callback in aws boto3 uploads (无法使其正常运行,但看起来很优雅)

  2. Track download progress of S3 file using boto3 and callbacks (这是供下载,但帮助我了解了 回调应该看起来像)

  3. S3 Python Download with Progress Bar (工作,我用这个来编写我的代码)。

这是我所做的工作:

import boto3
from botocore.exceptions import NoCredentialsError, ClientError
import os
import progressbar

ACCESS_KEY = 'MY_ACCESS_KEY'
SECRET_KEY = 'MY_SECRET_KEY_THAT_I_KNOW_SHOULD_NOT_BE_HERE_BUT_I_HAVENT_IMPLEMENTED_THE_PROPER_WAY_YET_ALSO_I_DO_NOT_KNOW_WHY_I_AM_YELLING_SORRY'

def upload_to_aws(local_file, s3_bucket, s3_folder, s3_filename,force_overwrite='n'):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                    aws_secret_access_key=SECRET_KEY)


    def write_to_aws():

        statinfo = os.stat(local_file)
        up_progress = progressbar.progressbar.ProgressBar(maxval=statinfo.st_size)
        up_progress.start()

        def upload_progress(chunk):
            up_progress.update(up_progress.currval + chunk)


        try:
            print("Writting")
            s3.upload_file(local_file, s3_bucket, s3_folder+s3_filename, Callback=upload_progress)
            print("Upload Successful")
            return True
        except FileNotFoundError:
            print("The source file was not found")
            return False
        except NoCredentialsError:
            print("Credentials not available")
            return False
    try:
        s3.head_object(Bucket=s3_bucket, Key=s3_folder+s3_filename)
        if force_overwrite=='y':
            write_to_aws
        else:
            ask_overwrite = input('File already exists at destination. Overwrite?')
            if ask_overwrite == 'y':
                write_to_aws()
            else:
                print('leaving application')
    except ClientError as e:
        write_to_aws()

我知道您可能已经知道了,但是如果其他人在寻找解决方案时发现了这一点,希望对您有所帮助