Go through the following documentation

http://boto3.readthedocs.io/en/latest/guide/migrations3.html

Creating the Connection

Boto 3 has both low-level clients and higher-level resources. For Amazon S3, the higher-level resources are the most similar to Boto 2.x's s3 module:

Boto 2.x

import boto

s3_connection = boto.connect_s3()

Boto 3

import boto3

s3 = boto3.resource('s3')

Creating a Bucket

Creating a bucket in Boto 2 and Boto 3 is very similar, except that in Boto 3 all action parameters must be passed via keyword arguments and a bucket configuration must be specified manually:

Boto 2.x

s3_connection.create_bucket('mybucket')

s3_connection.create_bucket('mybucket', location=Location.USWest)

Boto 3

s3.create_bucket(Bucket='mybucket')

s3.create_bucket(Bucket='mybucket', CreateBucketConfiguration={
    'LocationConstraint': 'us-west-1'})

Storing Data

Storing data from a file, stream, or string is easy:

Boto 2.x

from boto.s3.key import Key

key = Key('hello.txt')

key.set_contents_from_file('/tmp/hello.txt')

Boto 3

s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
Answer from Vaibhav Walke on Stack Overflow
🌐
AWS
docs.aws.amazon.com › boto3 › latest › reference › services › s3.html
S3 - Boto3 1.43.58 documentation
Resources are available in boto3 via the resource method. For more detailed instructions and examples on the usage of resources, see the resources user guide. ... The following example shows how to use an Amazon S3 bucket resource to list the objects in the bucket.
🌐
AWS
docs.aws.amazon.com › boto3 › latest › reference › services › s3 › bucket
Bucket - Boto3 1.43.32 documentation
Do you have a suggestion to improve this website or boto3? Give us feedback · S3 / Resource / Bucket
Top answer
1 of 4
27

Go through the following documentation

http://boto3.readthedocs.io/en/latest/guide/migrations3.html

Creating the Connection

Boto 3 has both low-level clients and higher-level resources. For Amazon S3, the higher-level resources are the most similar to Boto 2.x's s3 module:

Boto 2.x

import boto

s3_connection = boto.connect_s3()

Boto 3

import boto3

s3 = boto3.resource('s3')

Creating a Bucket

Creating a bucket in Boto 2 and Boto 3 is very similar, except that in Boto 3 all action parameters must be passed via keyword arguments and a bucket configuration must be specified manually:

Boto 2.x

s3_connection.create_bucket('mybucket')

s3_connection.create_bucket('mybucket', location=Location.USWest)

Boto 3

s3.create_bucket(Bucket='mybucket')

s3.create_bucket(Bucket='mybucket', CreateBucketConfiguration={
    'LocationConstraint': 'us-west-1'})

Storing Data

Storing data from a file, stream, or string is easy:

Boto 2.x

from boto.s3.key import Key

key = Key('hello.txt')

key.set_contents_from_file('/tmp/hello.txt')

Boto 3

s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))
2 of 4
10

First, in boto3, if you setup security using "aws configure" , you don't need to declare that "sess" section (http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)

# if you already done aws configure
import boto3
s3 = boto3.client("s3")
s3.create_bucket(Bucket="mybucket", ....) 

Second, is the bad boto3 documentation that fail to link proper information. This is found under boto3 pdf, page 2181 (https://media.readthedocs.org/pdf/boto3/latest/boto3.pdf)

Email : The value in the Grantee object is the registered email address of an AWS account.

Grantee : The AWS user or group that you want to have access to transcoded files and playlists. To identify the user or group, you can specify the canonical user ID for an AWS account, an origin access identity for a CloudFront distribution, the registered email address of an AWS account, or a predefined Amazon S3 group

And the easier solution is just use policy setting (http://support.cloudcheckr.com/getting-started-with-cloudcheckr/preparing-your-aws-account/aggregate-cloudtrail/) . You can convert the whole stuff using put_bucket_policy(), skip the dire GrantWrite,GrantWriteACP

🌐
Medium
medium.com › featurepreneur › control-aws-s3-using-boto3-df58e7038175
Control AWS S3 using Boto3. Introduction : | by SivaraamTK | featurepreneur | Medium
August 4, 2022 - Using the Boto3 library with Amazon Simple Storage Service (S3) allows you to create, update, and delete S3 Buckets, Objects, S3 Bucket Policies, and many more from Python programs or scripts with ease.
🌐
Weka
docs.weka.io › additional-protocols › s3 › s3-examples-using-boto3
S3 examples using boto3 | W E K A
May 18, 2026 - #!/usr/bin/env/python import boto3 import logging from botocore.exceptions import ClientError from botocore.client import Config config = Config( signature_version = 's3v4' ) s3 = boto3.resource('s3', endpoint_url='https://weka:9000', aws_access_key_id='s3_key', aws_secret_access_key='s3_secret', config=config) try: # upload a file from local file system 'myfile' to bucket 'mybucket' with 'my_uploaded_object' as the object name.
🌐
DEV Community
dev.to › aws-builders › how-to-list-contents-of-s3-bucket-using-boto3-python-47mm
How to List Contents of s3 Bucket Using Boto3 Python? - DEV Community
October 12, 2021 - Create the boto3 s3 client using the boto3.client('s3') method. Invoke the list_objects_v2() method with the bucket name to list all the objects in the S3 bucket.
🌐
Boto
boto.cloudhackers.com › en › latest › s3_tut.html
An Introduction to boto’s S3 interface — boto v2.49.0
Once you have a connection established with S3, you will probably want to create a bucket. A bucket is a container used to store key/value pairs in S3. A bucket can hold an unlimited amount of data so you could potentially have just one bucket in S3 for all of your information.
Find elsewhere
Top answer
1 of 16
186

Below piece of code returns ONLY the 'subfolders' in a 'folder' from s3 bucket.

import boto3
bucket = 'my-bucket'
#Make sure you provide / in the end
prefix = 'prefix-name-with-slash/'  

client = boto3.client('s3')
result = client.list_objects(Bucket=bucket, Prefix=prefix, Delimiter='/')
for o in result.get('CommonPrefixes'):
    print 'sub folder : ', o.get('Prefix')

For more details, you can refer to https://github.com/boto/boto3/issues/134

2 of 16
126

S3 is an object storage, it doesn't have real directory structure. The "/" is rather cosmetic. One reason that people want to have a directory structure, because they can maintain/prune/add a tree to the application. For S3, you treat such structure as sort of index or search tag.

To manipulate object in S3, you need boto3.client or boto3.resource, e.g. To list all object

import boto3 
s3 = boto3.client("s3")
all_objects = s3.list_objects(Bucket = 'bucket-name') 

http://boto3.readthedocs.org/en/latest/reference/services/s3.html#S3.Client.list_objects

In fact, if the s3 object name is stored using '/' separator. The more recent version of list_objects (list_objects_v2) allows you to limit the response to keys that begin with the specified prefix.

To limit the items to items under certain sub-folders:

    import boto3 
    s3 = boto3.client("s3")
    response = s3.list_objects_v2(
            Bucket=BUCKET,
            Prefix ='DIR1/DIR2',
            MaxKeys=100 )

Documentation

Another option is using python os.path function to extract the folder prefix. Problem is that this will require listing objects from undesired directories.

import os
s3_key = 'first-level/1456753904534/part-00014'
filename = os.path.basename(s3_key) 
foldername = os.path.dirname(s3_key)

# if you are not using conventional delimiter like '#' 
s3_key = 'first-level#1456753904534#part-00014'
filename = s3_key.split("#")[-1]

A reminder about boto3 : boto3.resource is a nice high level API. There are pros and cons using boto3.client vs boto3.resource. If you develop internal shared library, using boto3.resource will give you a blackbox layer over the resources used.

🌐
GitHub
github.com › boto › boto3
GitHub - boto/boto3: Boto3, an AWS SDK for Python · GitHub
5 days ago - >>> import boto3 >>> s3 = boto3.resource('s3') >>> for bucket in s3.buckets.all(): print(bucket.name) You can run tests in all supported Python versions using tox. By default, it will run all of the unit and functional tests, but you can also specify your own pytest options.
Author   boto
🌐
CodeSignal
codesignal.com › learn › courses › mastering-amazon-s3-with-aws-sdk-for-python › lessons › managing-aws-s3-objects-upload-download-and-delete-with-python-and-boto3
Managing AWS S3 Objects: Upload, Download, and Delete ...
Specify the bucket (cosmo-user-uploads), the object name within the bucket (cosmo-profile-2023.jpg), and the file's local path to upload. import boto3 s3_resource = boto3.resource('s3') s3_resource.Bucket('cosmo-user-uploads').upload_file('path/to/local/file.jpg', 'cosmo-profile-2023.jpg')
🌐
AWS re:Post
repost.aws › questions › QU0CffbvVeTTuO2yYrnRlurA › s3-modify-bucket-policy-using-boto-boto3
S3 modify bucket policy using boto/boto3 | AWS re:Post
March 9, 2024 - # Or use client to get Bucket policy s3_client = boto3.client('s3') policy = s3_client.get_bucket_policy(Bucket='vpc-flow-log-sudsark') # assign policy using s3 resource user_policy = { "Sid": "AutomatedRestrictiveAccess", "Effect": "Allow", "Principal": { "Service": "delivery.logs.amazonaws.com" }, "Action": "s3:PutObject", "Resource": "arn:aws:s3:::vpc-flow-log-sudsark/*", "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control", "aws:SourceAccount": "333344445555" } } } new_policy = policy['Statement'].append(user_policy) s3_client.put_bucket_policy(Policy=new_policy)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-list-s3-buckets-with-boto3
How To List S3 Buckets With Boto3 ? - GeeksforGeeks
July 23, 2025 - Basically here we have written a python code using boto3 module to list all the S3 buckets on AWS account .
🌐
AWS
docs.aws.amazon.com › boto3 › latest › guide › s3-example-creating-buckets.html
Amazon S3 buckets - Boto3 1.43.51 documentation
Do you have a suggestion to improve this website or boto3? Give us feedback. ... An Amazon S3 bucket is a storage location to hold files.
🌐
NashTech Blog
blog.nashtechglobal.com › home › how to perform different operations on  aws s3 bucket using boto3
How to perform different operations on AWS S3 bucket using Boto3 - NashTech Blog
May 25, 2022 - Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like Amazon S3 and Amazon EC2.
🌐
Amazon Web Services
boto3.amazonaws.com › v1 › documentation › api › latest › reference › services › s3 › bucket › objects.html
objects - Boto3 1.43.54 documentation
If the object specified in the request isn’t found, Amazon S3 confirms the deletion by returning the result as deleted. Directory buckets - S3 Versioning isn’t enabled and supported for directory buckets.
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › reading-from-an-s3-bucket-using-boto3-on-serverless-cluster › td-p › 115692
Reading from an S3 bucket using boto3 on serverles... - Databricks Community - 115692
November 11, 2025 - When I tried the new feature (Instance Profiles with Serverless), DBUTIL functions work great on Notebooks, but not the Boto3. We can use Spark read functions, but they are not meant for every operation we perform on S3.
🌐
AWS
learnaws.org › 2022 › 07 › 13 › boto3-upload-files-s3
How to use Boto3 to upload files to an S3 Bucket?
The upload_file method uploads a file to an S3 object. ... Filename (str) -- The path to the file to upload. Bucket (str) -- The name of the bucket to upload to. Key (str) -- The name of the key to upload to. ExtraArgs (dict) -- Extra arguments that may be passed to the client operation. For allowed upload arguments see boto3.s3.transfer.S3Transfer.ALLOWED_UPLOAD_ARGS.