gpt4 book ai didi

python - 使用 Boto3 回调,显示上传进度

转载 作者:行者123 更新时间:2023-12-04 17:34:57 25 4
gpt4 key购买 nike

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

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

查看此处的文档:https://boto3.amazonaws.com/v1/documentation/api/latest/_modules/boto3/s3/transfer.html我不明白如何显示上传调用的构建进度 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'))

最佳答案

SO中有三个相关的答案:

  • Get progress callback in aws boto3 uploads
    (无法使其工作,但看起来很优雅)
  • Track download progress of S3 file using boto3 and callbacks
    (这是用于下载,但帮助我了解了
    回调应该看起来像)
  • 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()
    我知道您可能已经想通了,但是如果其他人在寻找解决方案时发现了这一点,我希望这会有所帮助

    关于python - 使用 Boto3 回调,显示上传进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57065815/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com