- How to use parameters
- What is the difference between
To retrieve objects in an Amazon S3 bucket, the operation is
listObjects does not return the content of the object, but the key and meta data such as size and owner of the object.
To make a call to get a list of objects in a bucket:
params can be configured with the following parameters:
But what are
Prefix? And how to use them?
Let’s start by creating some objects in an Amazon S3 bucket similar to the following file structure. This can be easily done by using the AWS Console.
In Amazon S3, the objects are:
One thing to keep in mind is that Amazon S3 is not a file system. There is not really the concept of file and directory/folder. From the console, it might look like there are 2 directories and 3 files. But they are all objects. And objects are listed alphabetically by their keys.
To make it a little bit more clear, let’s invoke the
listObjects method. Since the operation has only
Bucket parameter is required:
data contains in the callback function:
If this is a file structure, you might expect:
But it is not, because a bucket does not work like a folder or a directory, where the immediate files inside the directory is shown. The objects inside the bucket are laid out flat and alphabetically.
In UNIX, a directory is a file, but in Amazon S3, everything is an object, and can be identified by key.
So, how to make Amazon S3 behave more like a folder or a directory? Or how to just list the content of first level right inside the bucket?
In order to make it work like directory you have to use
Delimiter is what you use to group keys. It does have to be a single character, it can be a string of characters. And
Prefix limits the response to keys that begin with the specified prefix.
Let’s start by adding the following delimiter:
You will get something more like a listing of a directory:
There are a directory
directory/ and a file
file. What happened was that the following objects except
file are grouped by the delimiter
So, result in:
This feels more like a listing of a directory or folder. But if we change
i, then, you get no
Contents and just the prefixes:
All keys can be grouped into two prefixes:
fi. Therefore, Amazon S3 is not a file system, but might act like one if using the right parameters.
As I have mentioned that
Delimiter does not need to be a single character:
If recall the bucket structure:
directory/directory/file are grouped into a common prefix:
directory/directory, due to the common grouping string
Let’s try another one with
Okay, one more. Let’s try
So, remember that
Delimiter is just providing a grouping functionality for keys. If you want it to behave like a file system, use
Prefix is much easier to understand, it is a filter that limits keys to be prefixed by the one specified.
With the same structure:
Let’s set the
Prefix parameter to
directory/ prefixes are the same.
If we try something slightly different,
Putting all together with both
Delimiter: 'directory' and
First, list the keys prefixed by
Group them by the delimiter
directory with prefix
i could give a better perspective:
One advantage of using Amazon S3 over listing a directory is that you don’t need to concern about nested directories, everything is being flattened. So, you can loop it through just by specifying the
If you’re using the Amazon AWS console to “Create Folder”, you can create a directory/folder and upload a file to inside the directory/folder. In effect, you are actually creating two objects with the following keys:
If you use the following command to upload a file, the directory is not created:
Because, Amazon S3 is not a file system, but a key/value store. If you use
listObjects method, you will just see one object. That is the same reason that you cannot copy a local directory:
Note that you must use
directory/ with trailing slash instead the one without. Otherwise, it is just a regular file not a directory.