[英]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.