Shell 脚本 - 如何将最后修改的 AWS S3 对象从一个存储桶复制到另一个存储桶?

Shell script - How to copy the last modified AWS S3 Object from one bucket to another?

我不熟悉编写脚本和编写 Shell 脚本来将 AWS S3 对象从一个 AWS 帐户存储桶复制到另一个存储桶?管道将 zip 文件 (.zip) 上传到存储桶。 shell 脚本应该只复制 最后修改的对象(文件)。我知道在 shell 脚本中,您可以基于通配符匹配递归查找文件并从 S3 CLI 获取最后上传的对象。有没有更优雅有效的方法呢?

# Normal way of copying from one bucket to another
aws s3 cp s3://source-bucket/object-v2.2.7.zip s3://destination- bucket/.

# Using the --recursive parameter to copy file
aws s3 ls $source-bucket --recursive | sort | tail -n 1 | awk '{print }'

  
get_object = `aws s3 ls $source-bucket --recursive | sort | tail -n 1 | awk '{print }'`

aws s3 cp s3://$get_object s3://destination-bucket
echo 'Successfully uploaded file to Destination Bucket'

我想你可以参考 来获取 S3 中最后修改的对象。

获得对象后,可以输入loop and inside the loop you can call aws s3 cp命令复制文件。如果它适合你,请告诉我。

此脚本找到最后修改的对象,然后复制它。

SOURCE_BUCKET=bucket1
DEST_BUCKET=bucket2

LAST=$(aws s3api list-objects --bucket $BUCKET --query 'sort_by(Contents, &LastModified)[-1].Key' --output text)

aws s3 cp s3://$SOURCE_BUCKET/$LAST s3://$DEST_BUCKET