简体   繁体   English

如何在 Python 中使用 boto3 检查 S3 上传的状态

[英]How to Check status of S3 upload using boto3 in Python

Here is my code for upload a file to S3 bucket sing boto3 in python.这是我在 python 中将文件上传到 S3 存储桶的代码。

import boto3

def upload_to_s3(backupFile, s3Bucket, bucket_directory, file_format):
    s3 = boto3.resource('s3')
    s3.meta.client.upload_file(backupFile, s3Bucket, bucket_directory.format(file_format))

upload_to_s3('/tmp/backup.py', 'bsfbackup', 'pfsense/{}', 'hello.py')

My question is about, I want to print the "Upload success" once the upload is succeeded, and print "Upload is failed" and the error stack if the uploading is failed.我的问题是,我想在上传成功后打印“上传成功”,如果上传失败,则打印“上传失败”和错误堆栈。 Any help ?有什么帮助吗?

Thanks.谢谢。

try:
   response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
   logging.error(e)
   return False
return True

to upload, u can use this alternative also,要上传,您也可以使用此替代方法,

The callback will give u the status of how many bytes of data has been transfer回调将为您提供传输了多少字节的数据的状态

from boto3.s3.transfer import S3Transfer
import threading 

def upload(file, input):
    transfer = S3Transfer(s3)
    size = float(os.path.getsize(file))
    transfer.upload_file(**input,
                         callback=_progress(file, size, 'Upload'))

def _progress(filename, size, ops):
    """ indicator to calculate progress based on filesize
    """
    _filename = filename
    _size = size
    _seen_so_far = 0
    _ops = ops
    _lock = threading.Lock()

    def call(bytes_amount):
        with _lock:
            nonlocal _seen_so_far
            _seen_so_far += bytes_amount
            percentage = (_seen_so_far / _size) * 100
            logging.info(
                "%s: %s  %s / %s  (%.2f%%)" % (
                    _ops, _filename, _seen_so_far, _size,
                    percentage))

    return call

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件上传到 S3 并使用 boto3 将其公开? - How to upload a file to S3 and make it public using boto3? 如何使用 Boto3 将空文件夹上传到 S3? - How to upload an empty folder to S3 using Boto3? 如何使用python boto3将文件上传到aws S3存储桶中的文件夹 - How to upload file to folder in aws S3 bucket using python boto3 如何在heroku上使用python中的boto3将文件上传到s3? - How do I upload a file to s3 using boto3 in python on heroku? 如何使用 python boto3 将嵌套目录和文件上传到 s3 存储桶 - How to upload nested directories and files into s3 bucket using python boto3 如何使用 Python 中的 S3 存储桶和 Boto3 将图像上传到 MongoDB - How to upload an image to MongoDB using an S3 bucket and Boto3 in Python 如何使用 lambda、boto3 和 python 2.7 检查 s3 的顶级文件夹中是否存在特定文件 - How to check if a particular file exists in a top level folder in s3, using lambda, boto3 and python 2.7 这是如何使用 boto3 python 在 s3 中标记两个 object - is this How to tag two object in s3 using boto3 python 如何使用 python boto3 重命名 s3 文件名 - How to rename the s3 file name by using python boto3 无法使用 python boto3 和 upload_fileobj 将文件上传到 AWS S3 - Unable to upload file to AWS S3 using python boto3 and upload_fileobj
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM