S3 Python下载进度B

2024-04-20 08:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

无法对我修改此代码的初始线程(Track download progress of S3 file using boto3 and callbacks)进行注释,因此希望有人能在这里帮助我。我可以用这段代码来显示文件上传的进度条,现在我需要对awss3的文件下载做同样的事情。任何帮助都将不胜感激!你知道吗

我知道我需要从S3而不是从本地文件系统获取文件的大小。我肯定有一些愚蠢的代码,我需要调整,使这项工作。希望有人能给我们一些启示。:)

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

Tags: 文件代码awsreturns3bucketlocaldef