使用awscli恢复中断的s3下载

时间:2017-07-12 23:04:44

标签: amazon-s3 aws-cli resume-download

我使用awscli下载文件:

$ aws s3 cp s3://mybucket/myfile myfile

但下载中断了(电脑进入睡眠状态)。我该如何继续下载? S3支持Range标头,但awscli s3 cp不允许我指定它。

该文件不可公开访问,因此我无法使用curl手动指定标题。

2 个答案:

答案 0 :(得分:8)

awscli工具中有一个“隐藏”命令,允许对S3进行较低级别的访问:s3api。†用户友好性较低(没有s3:// URL和没有进度条)但它确实支持get-object上的范围说明符:

   --range  (string) Downloads the specified range bytes of an object. For
   more   information   about   the   HTTP    range    header,    go    to
   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.

以下是继续下载的方法:

$ size=$(stat -f%z myfile) # assumes OS X. Change for your OS
$ aws s3api get-object \
            --bucket mybucket \
            --key myfile \
            --range "bytes=$size-" \
            /dev/fd/3 3>>myfile

您可以使用pv作为基本进度条:

$ aws s3api get-object \
            --bucket mybucket \
            --key myfile \
            --range "bytes=$size-" \
            /dev/fd/3 3>&1 >&2 | pv >> myfile

(这个未命名的管道问题的原因是s3api在操作结束时将调试消息写入stdout,污染了你的文件。这个解决方案将stdout重新绑定到stderr并通过别名释放管道以获取常规文件内容。没有pv的版本在技术上可以写入stderr(/dev/fd/22>),但如果发生错误,s3api会写入stderr,然后将其附加到您的文件中。因此,它是在那里使用专用管道也更安全。)

†在git中,s3是瓷器,而s3api是笨蛋。

答案 1 :(得分:1)

使用s3cmd,它具有内置的--continue函数。示例:

# Start a download
> s3cmd get s3://yourbucket/yourfile ./
download: 's3://yourbucket/yourfile' -> './yourfile' [1 of 1]
    123456789 of 987654321     12.5% in 235s   0.5 MB/s

[ctrl-c] interrupt

# Pick up where you left off
> s3cmd --continue get s3://yourbucket/yourfile ./
相关问题