---
title: "Rename S3 Folders Swiftly with Python and Boto SDK"
description: "Discover how to quickly rename S3 folder objects using Python and the powerful boto SDK."
canonical: "https://adamtheautomator.com/rename-s3-folder/"
---

# Rename S3 Folders Swiftly with Python and Boto SDK

> Discover how to quickly rename S3 folder objects using Python and the powerful boto SDK.

Source: https://adamtheautomator.com/rename-s3-folder/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Rename S3 Folders Swiftly with Python and Boto SDK](https://adamtheautomator.com/wp-content/uploads/2019/07/photo-1555949963-aa79dcee981c.jpg)

# Rename S3 Folders Swiftly with Python and Boto SDK

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)26 July 20193 min. read

Categories: [Cloud](/category/cloud/)

Tags:[AWS S3](/tag/aws-s3/)[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Rename S3 Folder Key with Boto](#renaming-an-amazon-s3-key)
*   [Summary](#summary)

To rename a folder on a traditional file system is a piece of cake but what if that file system wasn’t really a file system at all? In that case, it gets a little trickier! Amazon’s S3 service consists of objects with key values. To rename S3 folder objects, we still need to perform typical file system-like actions like renaming folders.

Renaming S3 “folders” isn’t possible; not even in the S3 management console but we can perform a workaround. We can create a new “folder” in S3 and then move all of the files from that “folder” to the new “folder”. Once all of the files are moved, we can then remove the source “folder”.

![S3 Buckets Containing Files to Rename S3 Folder Objects](https://adamtheautomator.com/wp-content/uploads/2022/04/image-121-1024x244.png)

S3 Buckets Containing Files to Rename S3 Folder Objects

To do this, use [Python](https://adamtheautomator.com/tag/python/) and the [boto3 module](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html). If you’re working with S3 and Python and not using the boto3 module, you’re missing out. It makes things _much_ easier to work with.

Related:[Getting Started: Managing AWS EC2 with Python Boto3](https://adamtheautomator.com/boto3-ec2/)

## Prerequisites

For the demonstration I’ll be showing you to work, you’ll need to meet a few prereqs ahead of time:

*   macOS/Linux
*   Python 3+
*   The boto3 module (_pip install boto3_ to get it)
*   An Amazon S3 Bucket
*   An AWS IAM user access key and secret access key with access to S3
*   An existing “folder” with “files” inside in your S3 bucket

Related:[Learning Identity and Access Management (IAM) AWS Through Examples](https://adamtheautomator.com/iam-aws/)

## Rename S3 Folder Key with Boto

To rename our S3 folder, we’ll need to import the _boto3_ module and I’ve chosen to assign some of the values I’ll be working with as variables.

```python
import boto3

awsAccessKey = ''
awsSecretAccessKey = ''
s3BucketName = ''
oldFolderKey = ''
newFolderKey = ''
```

Once I’ve done that, I’ll need to authenticate to S3 by providing my access key ID and secret key for the IAM user I’ll be using. In this case, I’ve chosen to use a _boto3_ session. I’ll be using a _boto3_ resource to work with S3.

```python
session = boto3.Session(aws_access_key_id=awsAccessKey,     aws_secret_access_key=awsSecretAccessKey)
s3 = session.resource('s3')
```

Once I’ve done that, I then need to find all of the files matching my key prefix to rename S3 folder. You can see below that I’m using a Python for loop to read all of the objects in my S3 bucket. I’m using the optional filter action and filtering all of the S3 objects in the bucket down to only eventually rename S3 folder I want.

```python
bucket = s3.Bucket(s3BucketName)
for object in bucket.objects.filter(Prefix=oldFolderKey):
```

Once I’ve started the [for loop](https://adamtheautomator.com/powershell-for-loop/ "for loop") iterating over the “folder” key and all of the “file” keys inside of it, I’ll then need to exclude the “folder” key itself since I won’t be copying that. I just need the file keys. I’m excluding that by an _if_ statement that matches all key values that don’t end with a forward slash.

After I’m in the block that will only contain file key values, I’m now assigning the file name and destination key names to make it easier to reference.

```python
for object in bucket.objects.filter(Prefix=oldFolderKey):
    srcKey = object.key
    if not srcKey.endswith('/'):
        fileName = srcKey.split('/')[-1]
        destFileKey = newFolderKey + '/' + fileName
        copySource = s3BucketName + '/' + srcKey         
        s3.Object(s3BucketName, destFileKey).copy_from(CopySource=copySource)
```

Once you have all of that setup, I then finally do the actual copy using the copy\_from action. You can see below that I’m creating an S3 object using the bucket name and destination file key. I’m then passing the source key to the _copy\_from_ action.

```python
for object in bucket.objects.filter(Prefix=oldFolderKey):
    srcKey = object.key
    if not srcKey.endswith('/'):
        fileName = srcKey.split('/')[-1]
        destFileKey = newFolderKey + '/' + fileName
        copySource = s3BucketName + '/' + srcKey         
        s3.Object(s3BucketName, destFileKey).copy_from(CopySource=copySource)
```

Once the loop has finished and all of the files have been copied to the new key, I’ll then need to use the delete action to clean all of the files including the “folder” key since it is not inside of the if condition.

```python
for object in bucket.objects.filter(Prefix=oldFolderKey):
    srcKey = object.key
    if not srcKey.endswith('/'):
        fileName = srcKey.split('/')[-1]
        destFileKey = newFolderKey + '/' + fileName
        copySource = s3BucketName + '/' + srcKey         
        s3.Object(s3BucketName, destFileKey).copy_from(CopySource=copySource)
        s3.Object(s3BucketName, srcKey).delete()
```

## Summary

At this point, we’re done! You should now see all of the files that were previously in the source key under the destination key with no sign of the source key!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Frename-s3-folder%2F&text=Rename%20S3%20Folders%20Swiftly%20with%20Python%20and%20Boto%20SDK)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Frename-s3-folder%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Frename-s3-folder%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2021/08/pankaj-patel-Ylk5n_nd9dA-unsplash-scaled.jpg)

### [How to Set Up An AWS S3 Static SSL Website](/aws-s3-static-ssl-website/)

Learn how to set up an AWS S3 static SSL website served securely using AWS Cloudfront in this step-by-step guide!

![](https://adamtheautomator.com/wp-content/uploads/2021/05/Getting-Started-Managing-AWS-EC2-with-Python-Boto3.jpg)

### [Take Advantage of Boto3 EC2 To Manage AWS EC2 instances](/boto3-ec2/)

Learn how to use the AWS Boto3 EC2 Python SDK to create new EC2 instances, start, stop, terminate and query tons of information about EC2.

![](https://adamtheautomator.com/wp-content/uploads/2021/04/Getting-Started-Managing-AWS-S3-with-Python-Boto3.jpg)

### [Utilizing Boto3 to Manager AWS S3](/boto3-s3/)

Learn how to get started from scratch on copying files with Python and AWS S3 using the Boto3 S3 Python module.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
