How to extract and manipulate all the .zip files stored in a folder of an Amazon S3 bucket. Part 2:- The Manipulation

How to extract and manipulate all the .zip files stored in a folder of an Amazon S3 bucket. Part 2:- The Manipulation

Ok, you have now fetched the zip files from the s3 location (If you have missed how to fetch the zip files from a S3 location, visit this blog to learn how to do so - https://bongtechblogger.hashnode.dev/how-to-extract-and-manipulate-all-the-zip-files-stored-in-a-folder-of-an-amazon-s3-bucket-part-1-the-extraction ).

What next ? The manipulation part. Suppose your problem statement requires you to fetch only the .csv files from each of the .zip files and upload the fetched zipped files into another s3 location. How can you achieve that ? Well, it's simple. You resort to using boto3 module again !!!

Only this time, you would be using boto3 resource rather than boto3 client. boto3 resource allows you to use the high level apis of boto3 module.

So, in order to fetch manipulate the .zip files and fetch only certain files from each of the .zip files, our code would look something like this.

import boto3
import zipfile
from io import BytesIO

bucket = 'bucket-name' 
prefix = "folder1/folder2/" # folder path of the zip files location 

s3_resource = boto3.resource('s3') # create an instance of boto3 resource 

bucket_dev = s3_resource.Bucket(bucket) 

# Iterating over each .zip file and converting them into a s3 resouce object for further manipulation 
for zip_file in zip_file_list: 
  zip_obj = s3_resource.Object(bucket_name=bucket, key=zip_file) 

  buffer = BytesIO(zip_obj.get()["Body"].read()) 
  z = zipfile.ZipFile(buffer) 

  for filename in z.namelist():
    if filename.endswith(".csv"):
      # Uploading files to another S3 location 
      dev_resource.meta.client.upload_fileobj(z.open(filename),Bucket=bucket,Key=prefix+"folder3/" + f'{filename}')

I hope the manipulation of zip files that are stored in S3 buckets would be a cakewalk for you guys now !!!

Happy Learning :)

Did you find this article valuable?

Support ABHIRUP DAS by becoming a sponsor. Any amount is appreciated!