How list Amazon S3 bucket contents by modified date?

后端 未结 8 1288
[愿得一人]
[愿得一人] 2020-12-14 06:30

Most of the time it happens that we load files in a common S3 bucket due to which it becomes hard to figure out data in it.

How can I view objects uploaded on a part

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 06:54

    If versioning is enabled for the bucket and you want to restore latest deleted objects after a specific date, this is the command:

    $ aws s3api list-object-versions --bucket mybucket --prefix myprefix/ --output json --query 'DeleteMarkers[?LastModified>=`2020-07-07T00:00:00` && IsLatest==`true`].[Key,VersionId]' | jq -r '.[] |  "--key '\''" + .[0] +  "'\'' --version-id " + .[1]' |xargs -L1 aws s3api delete-object --bucket mybucket
    

    That means that you have aws cli (I used v. 2.0.30) and jq installed.


    If you want to be sure before deleting that everything is ok, just use echo before aws:

    $ aws s3api list-object-versions --bucket mybucket --prefix myprefix/ --output json --query 'DeleteMarkers[?LastModified>=`2020-07-07T00:00:00` && IsLatest==`true`].[Key,VersionId]' | jq -r '.[] |  "--key '\''" + .[0] +  "'\'' --version-id " + .[1]' |xargs -L1 echo aws s3api delete-object --bucket mybucket > files.txt
    

    Note that because of echo, quotes will be not applied correctly and saved in the file without it. That's ok if there are no spaces in paths. You can check that file and if everything is ok, run in this way:

    $ cat files.txt | bash
    

提交回复
热议问题