0% found this document useful (0 votes)
8 views2 pages

S3 Zip File Extraction to CSV

Uploaded by

vinaygadade45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

S3 Zip File Extraction to CSV

Uploaded by

vinaygadade45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import os

import boto3
import zipfile
import io
import logging
import pandas as pd

logger = [Link]()
[Link]([Link])

s3_client = [Link]('s3')

def lambda_handler(event, context):


try:
[Link]("Received event: %s", event)

# Get the S3 bucket and object key from the event


bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']

# Download the zip file from S3


response = s3_client.get_object(Bucket=bucket, Key=key)
zip_data = response['Body'].read()

# Extract the zip file


with [Link]([Link](zip_data)) as zip_ref:
for file_name in zip_ref.namelist():
if 'vinay/rrf/RX' in file_name:
# Extract the file
with zip_ref.open(file_name) as file_data:
df = pd.read_csv(file_data, delimiter='|')

# Get header names from the corresponding Excel sheet


sheet_name = [Link]([Link](file_name))[0]
# Assuming file names match sheet names
header_file_key = 'excelfile/RxNorm_Header.xlsx'
# Change to your actual file name
header_df = read_excel_from_s3(bucket, header_file_key,
sheet_name)
if header_df is not None:
[Link] = header_df.iloc[:, 1].tolist() # Assuming
headers are in column B

# Convert DataFrame to CSV format


csv_data = df.to_csv(index=False)

# Upload the CSV file to another folder in the same S3 bucket


destination_key = f"rxfiles/{[Link](file_name)}.csv"
s3_client.put_object(Bucket=bucket, Key=destination_key,
Body=csv_data)

return {
'statusCode': 200,
'body': 'Extraction and file upload completed'
}
except Exception as e:
[Link]("Error: %s", e, exc_info=True)
return {
'statusCode': 500,
'body': f'Error: {e}'
}

def read_excel_from_s3(bucket, key, sheet_name):


try:
response = s3_client.get_object(Bucket=bucket, Key=key)
excel_bytes = response['Body'].read()
df = pd.read_excel([Link](excel_bytes), sheet_name=sheet_name)
return df
except Exception as e:
[Link]("Error reading Excel file: %s", e)
return None

You might also like