Search code examples
python-3.xamazon-s3boto3bucket

How do I create a bucket and multiple subfolders at once in AWS S3 using Boto3?


I'm able to create one directory at the top level using

s3.create_bucket(Bucket=bucket_name)

I want to create a new bucket and subfolders so I have a directory structure like:

-top_level_bucket
    -sub_folder
        -sub_sub_folder

I want to do something like this to create everything at once if not already existent:

path = 'top_level_bucket/sub_folder/sub_sub_folder'
s3.create_bucket(Bucket=path)

Is this possible?


Solution

  • There is no concept of a 'sub-bucket' in Amazon S3.

    Amazon S3 is actually a flat object storage service. It does not use directories.

    Instead, files are uploaded with a path, eg:

    aws s3 cp file.txt s3://my-bucket/bob/files/file.txt
    

    The full name of the object will be: bob/files/file.txt

    It looks and behaves like there are directories, but they are not actually there. In fact, you can run the above command and it will automatically 'create' the bob and files directory, but they are not actually there! If you delete the object, those directories will disappear (because they were never actually there!).

    Bottom line: Upload files to where ever you wish, even if the buckets do not exist. Don't worry about creating a folder structure in advance.