import boto3 import pandas as pd from io import BytesIO # from dotenv import load_dotenv # # load_dotenv() def list_files_in_s3_folder(bucket_name, folder_path): """ List all files in a specified S3 folder. Args: bucket_name (str): The name of the S3 bucket. folder_path (str): The path to the folder in the S3 bucket. Returns: list: A list of file names in the specified S3 folder. """ s3_client = boto3.client('s3') # List objects in the specified S3 folder result = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=folder_path) if 'Contents' not in result: return [] # Extract the file names file_names = [item['Key'] for item in result['Contents']] return file_names def read_file_from_s3(bucket_name, file_key): """ Read the content of a file from S3 and return it as a byte stream. Args: bucket_name (str): The name of the S3 bucket. file_key (str): The key (path) to the Excel file in the S3 bucket. Returns: BytesIO: The content of the file as a BytesIO. """ s3_client = boto3.client('s3') try: # Download the file content from S3 response = s3_client.get_object(Bucket=bucket_name, Key=file_key) # Read the file content into a pandas DataFrame file_content = response['Body'].read() # Use BytesIO to read the file content in-memory s3_file = BytesIO(file_content) return s3_file except Exception as e: print(f"Error reading file from S3: {e}") return None