def download_s3(bucket_name, file_key, file_path, force=False):
file_path = path(file_path)<EOL>bucket = open_s3(bucket_name)<EOL>file_dir = file_path.dirname()<EOL>file_dir.makedirs()<EOL>s3_key = bucket.get_key(file_key)<EOL>if file_path.exists():<EOL><INDENT>file_data = file_path.bytes()<EOL>file_md5, file_md5_64 = s3_key.get_md5_from_hexdigest(hashlib.md5(file_data).hexdigest())<EOL>try:<EOL><INDENT>s3_md5 = s3_key.etag.replace('<STR_LIT:">', '<STR_LIT>')<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if s3_md5 == file_md5:<EOL><INDENT>info('<STR_LIT>' % file_path)<EOL>return<EOL><DEDENT>elif not force:<EOL><INDENT>s3_datetime = datetime.datetime(*time.strptime(<EOL>s3_key.last_modified, '<STR_LIT>')[<NUM_LIT:0>:<NUM_LIT:6>])<EOL>local_datetime = datetime.datetime.utcfromtimestamp(file_path.stat().st_mtime)<EOL>if s3_datetime < local_datetime:<EOL><INDENT>info("<STR_LIT>" % (file_key))<EOL>return<EOL><DEDENT><DEDENT><DEDENT><DEDENT>info("<STR_LIT>" % (file_key))<EOL>try:<EOL><INDENT>with open(file_path, '<STR_LIT:w>') as fo:<EOL><INDENT>s3_key.get_contents_to_file(fo)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>error("<STR_LIT>" % e)<EOL>raise<EOL><DEDENT>
Download a remote file from S3.