title: How do I download a file from S3 using boto only if the remote file is newer than a local copy?
body: <p>I'm trying to download a file from S3 using boto, but only if a local copy of the file is older than the remote file.</p>
  
  <p>I'm using the header 'If-Modified-Since' and the code below:</p>
  
  <pre><code>#!/usr/bin/python
  import os
  import datetime
  import boto
  from boto.s3.key import Key
  
  bucket_name = 'my-bucket'
  
  conn = boto.connect_s3()
  bucket = conn.get_bucket(bucket_name)
  
  def download(bucket, filename):
      key = Key(bucket, filename)
      headers = {}
      if os.path.isfile(filename):
          print "File exists, adding If-Modified-Since header"
          modified_since = os.path.getmtime(filename)
          timestamp = datetime.datetime.utcfromtimestamp(modified_since)
          headers['If-Modified-Since'] = timestamp.strftime("%a, %d %b %Y %H:%M:%S GMT")
      try:
          key.get_contents_to_filename(filename, headers)
      except boto.exception.S3ResponseError as e:
          return 304
      return 200
  
  print download(bucket, 'README')
  </code></pre>
  
  <p>The problem is that when the local file does not exist everything works well and the file is downloaded. When I run the script for the second time my function returns 304 as expected, but the file that was previously downloaded is deleted.</p>