0

我想使用 s3cmd 接口从 s3 下载文件。我正在使用命令:

s3cmd get s3://db-backups/db/production_dump_2013-09-12_12-00.sql.gz dump1.sql.g

该命令工作正常。接下来,我想自动化下载文件的任务。目录中有多个名称相似的文件,仅时间戳不同,例如:

production_dump_2013-09-12_09-00.sql.gz
production_dump_2013-09-12_12-00.sql.gz
production_dump_2013-09-12_15-00.sql.gz
production_dump_2013-09-12_18-00.sql.gz
production_dump_2013-09-12_21-00.sql.gz

如何下载最新的文件?如果文件名已知,那么我可以使用:

cmd = 's3cmd get s3://voylladb-backups/db/production_dump_2013-09-12_12-00.sql.gz dump1.sql.gz'
args = shlex.split(cmd)
p=subprocess.Popen(args)
p.wait()

如何修改它(或使用其他方法)以获取具有最新时间戳的文件?

谢谢

4

1 回答 1

1

您可以使用s3cmd ls s3://voylladb-backups/db/.

然后假设您返回一个列表,您可以将其反向排序并获取第一项。这可能不是写这个的最简洁的方式,但它应该可以工作:

import subprocess, re

# Use subprocess.check_output to get the output from the terminal command
lines = subprocess.check_output("s3cmd ls s3://voylladb-backups/db/".split(" ")).split("\n")

# the format is a bit weird so we want to isolate just the s3:// paths
# we'll use a regex search to find the s3:// pattern and any subsequent characters
file_re = re.compile("s3://.+")
files = []

# next we iterate over each line of output from s3cmd ls looking for the s3 paths
for line in lines:
    result = file_re.search(line)
    if result:
        # and add them to our list
        files.append(result.group(0))

# finally, reverse the list so the newest file is first, and grab the first item
files.sort(reverse=True)
print files[0] # production_dump_2013-09-12_21-00.sql.gz
于 2013-09-12T09:25:18.367 回答