Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)
I am using the AWS SDK for python Boto3. And I want to create folders directly using it. Is there any template or sample code which I could use? Please reply to me.

2 Answers

0 votes
by (44.4k points)

Yes, there is. It is just 5 lines of code where one line is importing boto3.

import boto3

s3 = boto3.client('s3')

bucket_name = "bucket-name-here"

folder_name = "name/ofyour/folders"

s3.put_object(Bucket=bucket_name, Key=(folder_name+'/'))

In folder_name, provide all the folder names you want to add.

Hope this helps!

Using boto3, how can I create a folder inside S3?
Intellipaat-community
by
thanks @kodee this is what I was looking for
0 votes
ago by (1.9k points)

There is no "folder" functionality native to AWS S3 or other traditional file systems. In the case of S3, a flat topology is employed, where folders can be defined by prefixes. In Boto3, creating a folder in S3 can be done by simply appending a forward slash to the end of an object's key.

Steps for folder creation with Boto3:

import boto3

# Create an S3 client

s3 = boto3.client('s3')

# Replace 'your-bucket-name' with your S3 bucket name and 'your-folder-name/' with your desired folder name

bucket_name = 'your-bucket-name'

folder_name = 'your-folder-name/'

# Create the folder by uploading an empty object

s3.put_object(Bucket=bucket_name, Key=folder_name)

print(f"Folder '{folder_name}' created successfully in bucket '{bucket_name}'")

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...