s3upload.py 文件源码

python
阅读 37 收藏 0 点赞 0 评论 0

项目:aws-ec2rescue-linux 作者: awslabs 项目源码 文件源码
def s3upload(presigned_url, filename):
    """
    Given an uploader URL and a filename, parse the URL and get the presigned URL then use the presigned URL to upload
    the file named filename to S3.

    Parameters:
        presigned_url (str): the presigned URL that that is the PUT endpoint
        filename (str): the file to be pushed to S3

    Returns:
        (bool): True if the presigned URL was generated and the file uploaded successfully, else False.
    """
    import logging
    ec2rlcore.logutil.LogUtil.get_root_logger().addHandler(logging.NullHandler())
    try:
        # The response puts the URL string in double quotes so cut off the first and last characters
        with open(filename, "rb") as payload:
            response = requests.put(url=presigned_url, data=payload)
            if response.status_code == 200:
                ec2rlcore.dual_log("Upload successful")
                return True
            else:
                ec2rlcore.dual_log("ERROR: Upload failed.  Received response {}".format(
                    response.status_code))
                raise S3UploadResponseError(response.status_code)
    except requests.exceptions.Timeout:
        raise requests.exceptions.Timeout("ERROR: connection timed out.")
    except IOError:
        raise S3UploadTarfileReadError("ERROR: there was an issue reading the file '{}'.".format(filename))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号