首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有进度条的S3 Python下载

带有进度条的S3 Python下载
EN

Stack Overflow用户
提问于 2020-01-01 10:37:44
回答 1查看 833关注 0票数 0

我不能评论我改编这段代码(Track download progress of S3 file using boto3 and callbacks)的初始线程,所以希望有人能在这里帮助我。我可以使用此代码来显示文件上传的进度条,现在我需要从亚马逊网络服务S3下载文件做同样的事情。任何帮助都将不胜感激!

我知道我需要从S3而不是从本地文件系统获取文件的大小。我确信我需要调整一些愚蠢的代码才能让它正常工作。希望有人能给我们一些启示。:)

代码语言:javascript
运行
复制
def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=s3ak,
                      aws_secret_access_key=s3sk)

    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:
        s3.upload_file(local_file, bucket, s3_file, Callback=upload_progress)
        up_progress.finish()
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False
EN

回答 1

Stack Overflow用户

发布于 2021-08-12 08:51:11

代码语言:javascript
运行
复制
import os
import boto3
import progressbar
from botocore.exceptions import NoCredentialsError


def download_from_s3(bucket, s3_file, local_file):
    s3 = boto3.client('s3')
    response = s3.head_object(Bucket=bucket, Key=s3_file)
    size = response['ContentLength']
    up_progress = progressbar.progressbar.ProgressBar(maxval=size)
    up_progress.start()

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

    try:
        s3.download_file(bucket, s3_file, local_file, 
Callback=upload_progress)
        up_progress.finish()
        print("Download Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False


resp = download_from_s3(bucket, s3_file, local_file)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59549681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档