Bootstrap FreeKB - Amazon Web Services (AWS) - List files in an S3 Bucket using AWS CLI
Amazon Web Services (AWS) - List files in an S3 Bucket using AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

An S3 Bucket is similar to an NFS share in that it is a mountable storage volume.

The aws s3api list-buckets command can be used to list your S3 buckets.

~]$ aws s3api list-buckets
{
    "Buckets": [
        {
            "Name": "my-bucket-abcdefg",
            "CreationDate": "2023-06-02T02:22:19+00:00"
        }
    ],
    "Owner": {
        "DisplayName": "john.doe",
        "ID": "ab0e0a411234d5103a77c82240d5abcdc41ff11cc325c65b5c777a5123443743"
    }
}

 

The aws s3api list-objects command can be used to list the files in your S3 Bucket.

aws s3api list-objects --bucket my-bucket-abcdefg

 

The --query option can be used to only list files that match a pattern.

aws s3api list-objects --bucket my-bucket-abcdefg --query 'Contents[?Key==`foo.txt`]'

 

Something like this should be returned.

[
    {
        "Key": "foo.txt",
        "LastModified": "2023-06-08T01:21:59+00:00",
        "ETag": "\"b1946ac9249abdg47c6235b4d1234184\"",
        "Size": 6,
        "StorageClass": "STANDARD",
        "Owner": {
            "DisplayName": "john.doe",
            "ID": "ab0e0a41e3abcd103a77c8224012343fc41ff11cc325c65b5c7abcgf8e743743"
        }
    }
]

 

And here is how you can list the name of each file in the S3 Bucket.

~]$ aws s3api list-objects --bucket my-bucket-abcdefg --query 'Contents[].{Key: Key}'
[
    {
        "Key": "example.txt"
    },
    {
        "Key": "foo.txt"
    },
    {
        "Key": "my-directory/bar.jpg"
    }
]

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter a90235 in the box below so that we can be sure you are a human.