Skip to Content

2 ways to Find a file in AWS S3 bucket

If you are a developer or sysadmin, you are likely to have worked with Amazon S3 at some point. S3 is a great storage option for static files, and it’s very easy to use. In this blog post, we will discuss two ways how to find a file in an S3 bucket.

What is an S3 bucket in AWS?

An S3 bucket is a simple storage container that can store an unlimited number of objects, up to 5TB in size each. Buckets are used to store objects that are related to one another. For example, you could have a bucket called “images” in which you store all of the images for your website.

  • Buckets – A bucket is a container for storing objects. All objects in an S3 bucket must have a unique name, and buckets can only be used to store objects in one region. You can use the aws s3 ls command to list all the buckets in a region.
  • Objects – An object is a file or folder stored in an S3 bucket. Objects are stored in folders, and folders can contain other folders and files. To get information about an object, you can use the aws s3 ls command.
  • Folders – A folder is a container for storing objects. You can create folders in an S31 bucket to organize your objects. To list all the files in a folder, you can use the aws s3 ls command.

 

Find a file in S3 bucket with AWS CLI

The best way to find a file in an S3 bucket is to use the AWS Command Line Interface (CLI). To do this, simply open a terminal window and type the following command:

Boost Your Website Speed!

If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!

aws s3 ls s3://your-bucket --recursive --human-readable --summarize | grep filename

The output of the command shows the date the objects were created, their file size, and their path. The parameters we’ve passed to the s3 ls command are:

  • 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

Find a file in a Folder of an S3 Bucket with AWS CLI

To find a file, located in a folder of an S3 bucket, we can pass in the entire path to the folder, and set the –recursive parameter in AWS s3 ls command. The following command only searches the files in the /YOUR_FOLDER directory.

aws s3 ls s3://YOUR_BUCKET/YOUR_FOLDER/ --recursive --human-readable --summarize | grep filename

Find a file in S3 bucket with AWS s3api list-objects

The AWS S3 API is a web service that lets you manage your Amazon S3 buckets and objects. aws s3api list-objects returns some or all of the objects in a bucket. You can use the request parameters as selection criteria to return a subset of the objects in a bucket.

list-objects is a paginated operation. Multiple API calls may be issued in order to retrieve the entire data set of results.

The following example uses the list-objects command to display the names of all the objects in the specified bucket. The example uses the –query argument to filter the output list-objects down to the key value and size for each object.

aws s3api list-objects --bucket <bucket-name>--query 'Contents[].{Key: Key, Size: Size}'

For the following example, we will search for a file name containing 1018441.

aws s3api list-objects --bucket <bucket-name> --query "Contents[?contains(Key, '1018441')]"