AWS S3 download and copy

时间:2016-07-28 20:28:04

标签: amazon-web-services amazon-s3 aws-cli

We have a bucket in AWS S3 where backups from production are being copy to.

My task is to copy the most recent backup file from AWS S3 to the local sandbox SQL Server, then do the restore.

I have installed all of the AWS tools for windows on the local server. Credentials to connect to AWS S3 work, etc. My local server can list all of the files in the AWS S3 bucket. I can successfully download a single file if I specifically name that file.

Here is an example of that working pulling the most recent copy from July 25, 2016.

aws s3 cp s3://mybucket/databasefile_20160725.zip E:\DBA 

My goal is to have a copy script that only pulls the most recent file, which I won't know the name of. I want to schedule this.

Nothing I google or try is getting me the correct syntax to do this.

1 个答案:

答案 0 :(得分:3)

要检索存储桶中的最新文件,您可以执行以下操作

aws s3api list-objects --bucket "mybucket" |\
jq '.Contents | sort_by(.LastModified) | .[-1].Key' --raw-output

第一个命令将在Json中列出存储桶的对象,列出了JSon的元素here 然后,您希望从上次修改日期对元素进行排序,获取最后一个元素,并且您希望它们为Key(即存储桶中文件的名称)。将--raw-output标志添加到键名称

中的条带引号

您可以在脚本中重复使用它,或者使用s3 cp命令将其重命名,如下所示

aws s3api list-objects --bucket "mybucket" |\
jq '.Contents | sort_by(.LastModified) | .[-1].Key' --raw-output |\
xargs -I {} aws s3 cp s3://mybucket/{} E:\DBA