【发布时间】:2014-10-02 14:35:46
【问题描述】:
我正在尝试使用 boto 从 S3 下载文件,但前提是该文件的本地副本比远程文件旧。
我正在使用标题“If-Modified-Since”和下面的代码:
#!/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')
问题是当本地文件不存在时,一切正常并且文件被下载。当我第二次运行脚本时,我的函数按预期返回 304,但之前下载的文件被删除了。
【问题讨论】: