How to display only files from aws s3 ls command?

后端 未结 9 792
北恋
北恋 2021-01-01 08:09

I am using the aws cli to list the files in an s3 bucket using the following command (documentation):

aws s3 ls s3://mybucket --recursive --human-readable --         


        
相关标签:
9条回答
  • 2021-01-01 09:02

    My Solution

    List only files recursively using aws cli.

    aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .
    

    grep . - Clear empty lines.


    Example: aws s3 ls s3://myBucket

                               PRE f5c10c1678e8484482964b8fdcfe43ad/
                               PRE f65b94ad31734135a61a7fb932f7054d/
                               PRE f79b12a226b542dbb373c502bf125ffb/
                               PRE logos/
                               PRE test/
                               PRE userpics/
    2019-05-14 10:56:28       7754 stage.js
    

    Solution: aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .

    stage.js
    
    0 讨论(0)
  • 2021-01-01 09:03

    Use the s3api with jq (AWS docu aws s3api list-objects):

    This mode is always recursive.

    $ aws s3api list-objects --bucket "bucket" | jq -r '.Contents[].Key'
    a.txt
    foo.zip
    foo/bar/.baz/a
    [...]
    

    You can filter sub directories by adding a prefix (here foo directory). The prefix must not start with an /.

    $ aws s3api list-objects --bucket "bucket" --prefix "foo/" | jq -r '.Contents[].Key'
    foo/bar/.baz/a
    foo/bar/.baz/b
    foo/bar/.baz/c
    [...]
    

    jq Options:

    • -r = Raw Mode, no quotes in output
    • .Contents[] = Get Contents Object Array Content
    • .Key = Get every Key Field (does not produce a valid JSON Array, but we are in raw mode, so we don't care)

    Addendum:

    You can use pure AWS CLI, but the values will be seperated by \x09 = Horizontal Tab (AWS: Controlling Command Output from the AWS CLI - Text Output Format)

    $ aws s3api list-objects --bucket "bucket" --prefix "foo/" --query "Contents[].Key" --output text
    foo/bar/.baz/a   foo/bar/.baz/b   foo/bar/.baz/c   [...]
    

    AWS CLI Options:

    • --query "Contents[].Key" = Query Contents Object Array and get every Key inside
    • --output text = Output as Tab delimited Text with now Quotes

    Addendum based on Guangyang Li Comment:

    Pure AWS CLI with New Line:

    $ aws s3api list-objects --bucket "bucket" --prefix "foo/" --query "Contents[].{Key: Key}" --output text
    foo/bar/.baz/a
    foo/bar/.baz/b
    foo/bar/.baz/c
    [...]
    
    0 讨论(0)
  • 2021-01-01 09:07

    Simple command would be

    aws s3 ls s3://mybucket --recursive --human-readable --summarize |cut -d ' ' -f 8
    

    If you need the timestamp, just update command field values.

    0 讨论(0)
提交回复
热议问题