Borislav Hadzhiev
Tue Sep 21 2021·3 min read
Photo by Mariana Vusiatytska
To list all of the files of an S3 bucket with the AWS CLI, use the s3 ls
command, passing in the --recursive
parameter.
aws s3 ls s3://YOUR_BUCKET --recursive --human-readable --summarize
The output of the command shows the date the objects were created, their file size and their path.
1,000
list requests is $0.005
in the us-east-1
region.The parameters we've passed to the s3 ls command are:
Name | Description |
---|---|
recursive | performs the command on all files under the set prefix |
human-readable | displays the file sizes in human readable format |
summarize | displays the total number of objects and their total size |
--recursive
parameter from the command.To list all files, located in a folder of an S3 bucket, use the s3 ls
command, passing in the entire path to the folder and setting the --recursive
parameter.
aws s3 ls s3://YOUR_BUCKET/YOUR_FOLDER/ --recursive --human-readable --summarize
The output of the command only shows the files in the /my-folder-1
directory.
To only list the filenames of an S3 bucket, we have to:
s3api list-objects
command--output
parameter to text
--query
parameter to filter the output from the commandaws s3api list-objects --bucket YOUR_BUCKET --output text --query "Contents[].{Key: Key}"
The output of the command shows all of the filenames, or in other words - path names in the S3 bucket.
To list only the filenames in a specific folder, add the --prefix
parameter
to the command:
aws s3api list-objects --bucket YOUR_BUCKET --prefix "my-folder/" --output text --query "Contents[].{Key: Key}"
We can also redirect the output to a file on the local file system:
aws s3api list-objects --bucket YOUR_BUCKET --prefix "my-folder-1/" --output text --query "Contents[].{Key: Key}" > file-names.txt