From af97f2a5ee9cdcf73acce0a9c15cdabc78c3d335 Mon Sep 17 00:00:00 2001 From: Jim Wilcoxson Date: Mon, 24 Dec 2012 10:27:04 -0500 Subject: [PATCH] S3: cleanup if key.get_contents_to_filename fails if key.get_contents_to_filename fails, it leaves an empty file and open file descriptor behind. Instead, remove the file (since we created it, or at least truncated it to 0 length) and close the file descriptor. --- boto/s3/key.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/boto/s3/key.py b/boto/s3/key.py index 99b7214c51..53bbe1af2f 100644 --- a/boto/s3/key.py +++ b/boto/s3/key.py @@ -1468,11 +1468,16 @@ def get_contents_to_filename(self, filename, headers=None, http://goo.gl/EWOPb for details. """ fp = open(filename, 'wb') - self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent, - version_id=version_id, - res_download_handler=res_download_handler, - response_headers=response_headers) - fp.close() + try: + self.get_contents_to_file(fp, headers, cb, num_cb, torrent=torrent, + version_id=version_id, + res_download_handler=res_download_handler, + response_headers=response_headers) + except Exception: + os.remove(filename) + raise + finally: + fp.close() # if last_modified date was sent from s3, try to set file's timestamp if self.last_modified != None: try: