【问题标题】:Shell script - How to copy the last modified AWS S3 Object from one bucket to another?Shell 脚本 - 如何将最后修改的 AWS S3 对象从一个存储桶复制到另一个存储桶?
【发布时间】:2020-12-12 07:34:33
【问题描述】:

我不熟悉编写脚本和编写 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 $4}'

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

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

【问题讨论】:

  • 请注意,列出一个大桶中的所有对象是一项缓慢且昂贵的操作,并且不保证 s3 在写入一致后被读取。
  • @DanielFarrell 很高兴知道,您对如何更优雅地执行此操作有什么建议吗?

标签: bash amazon-web-services shell amazon-s3 scripting


【解决方案1】:

此脚本查找最后修改的对象,然后将其复制。

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

【讨论】:

    【解决方案2】:

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

    一旦你得到了对象,你可以放一个loop并且在循环中你可以调用aws s3 cp命令来复制文件。如果它适合你,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 2021-04-13
      • 2019-04-29
      • 2018-11-22
      • 2023-02-01
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多