Reputation: 2773
I am working with Python Boto to work with S3. Let's say the name of the bucket is... bucket.
In bucket, I have a directory a. Within a, I have many other directories one, two, three... Within each of the directories one, two, three, etc., I have many files.
I am attempting to check if a directory such as a/x exists. I have tried something like this:
key = bucket.get_key('/a/one/')
but no luck. My workaround has been to return
list(bucket.list("a/", "/"))
and see if x exists in the list, but it seems messy. Also, within the returned list, i have 'a/' as a key. If 'a/', a directory, is a key, then shouldn't 'a/one/' also be one?
What is the best way of doing this? Appreciate any help.
Upvotes: 0
Views: 2121
Reputation: 31349
S3 does not have the concept of a directory. A directory is normally considered "existing" if there is a key with a path the same as the directory, but this example of a single key in a bucket is perfectly valid:
'/path/to/file/with/no/dir/file.txt'
Notice there are no key entries for:
'/path/'
'/path/to/'
Although there could be, but it has to be managed.
What you could do is see if any of the keys resides in the directory you're checking, something like:
dir_exists = any([k.startswith(path) for k in s3_file_names]),
Otherwise you have to maintain directory files in the bucket.
Upvotes: 4