S3 Python Download with Progress Bar

≯℡__Kan透↙ 提交于 2021-01-29 08:55:09

问题


Couldn't comment on the initial thread where I adapted this code (Track download progress of S3 file using boto3 and callbacks) so hopefully someone can help me here. I was able to use this code to show a progress bar for file uploads, now I need the do the same thing for file downloads from AWS S3. Any help would be GREATLY APPRECIATED!

I know I need to get the size of the file from S3 instead of from the local file system. I'm sure there is some silly code I need to adjust to make this work. Hopefully, someone can shed some light. :)

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

来源:https://stackoverflow.com/questions/59549681/s3-python-download-with-progress-bar

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