Python S3 full class — with full functionality, based on boto3

Tamir Ciprut
2 min readDec 6, 2020

--

With this class you can rename & move objects in AWS S3, or even loop through more than 1k objects.

Have you ever asked yourself how to rename object in AWS S3 using boto3?
Or, How to copy object from one bucket to another using boto3?

Or more frequently, You don’t now what to do when you get only first 1000 objects when you use boto3?

Get this class for yourself and use all you need.

initialize class

import boto3
import os
class S3Client:
def __init__(self):
self.client = boto3.client(‘s3’)

get list of keys (when you got more than 1000)

def get_keys_list(self, bucket_name, prefix, max_amount, suffix):
paginator = self.client.get_paginator(‘list_objects_v2’)
pages = paginator.paginate(
Bucket=bucket_name,
Prefix=prefix,
PaginationConfig={‘MaxItems’: max_amount})
object_keys = []

for page in pages:
for obj in page[‘Contents’]:
obj_key = obj[‘Key’]
if obj_key.endswith(suffix):
object_keys.append(obj_key)

return object_keys

download file

def download_file(self, bucket_name, object_key, destination_file_path):
self.client.download_file(
bucket_name,
object_key,
destination_file_path
)

download list of keys

def download_list_of_keys(self, bucket_name, list_of_keys, destination_folder):    for obj_key in list_of_keys:        # get only filename and not whole path in s3
_, dest_file_name = os.path.split(obj_key)

self.client.download_file(
bucket_name,
obj_key,
os.path.join(destination_folder, dest_file_name
)

move object

def move_file(self, origin_bucket, origin_object_key, destination_bucket, destination_object_key):

# copy to destination
self.copy(
origin_bucket,
origin_object_key,
destination_bucket,
destination_object_key
)
# delete the original
self.client.delete_object(
Bucket=origin_bucket,
Key=origin_object_key
)

copy object

def copy(self, origin_bucket, origin_object_key, destination_bucket, destination_object_key):
# copy to destination
copy_source = {
‘Bucket’: origin_bucket,
‘Key’: origin_object_key
}
self.client.copy(
copy_source,
destination_bucket,
destination_object_key
)

upload file

def upload_file(self, file_local_path, bucket_name, destination_key):

self.client.upload_file(
file_local_path,
bucket_name,
destination_key
)

upload list of files

def upload_list_of_files(self, list_of_paths, bucket_name, prefix):

for file_local_path in list_of_paths:
filename = self.get_file_name_from_path(file_local_path)
destination_key = os.path.join(prefix, filename)
self.upload_file(
file_local_path=file_local_path,
bucket_name=bucket_name,
destination_key = destination_key
)

rename object

def rename_file(self, bucket_name, origin_object_key, new_object_key):    # copy
self.copy(
bucket_name,
origin_object_key,
bucket_name,
new_object_key
)

# delete the original
self.client.delete_object(
Bucket=origin_bucket,
Key=origin_object_key
)

Do you have something to add or to make better?
PLEASE FEEL FREE TO SUGGEST ANYTHING.

--

--

Tamir Ciprut
Tamir Ciprut

No responses yet