Amazon S3 uses is "flat" storage system. It does not actually support folders/sub-directories. Rather, the sub-directory path is stored as part of the filename, known as the "key".
For example, foo.txt
stored in a folder called bar
actually creates an object with a key name of bar/foo.txt
. When retrieving the object it appears as though it is in the bar directory, but it really isn't.
S3 does support the ability to retrieve a list of Common Prefixes that is effectively a list of directories, but there is no guarantee that such prefixes always exist, especially for empty directories.
Rather than writing a script to "upload folders if not found", take a look at the AWS Command-Line Interface (CLI). It includes a command aws s3 sync
that can automatically copy files between locations, including only copying new or modified files. That might totally avoid the need for you to write your own script.