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import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Define AWS credentials and S3 resources
|
| 6 |
+
ACCESS_KEY = "AKIAZPNYOGSLLAIPOMM2"
|
| 7 |
+
SECRET_KEY = "atM6s86u9okn1U3UpOGqkvMuyDvKGAYYYeXcjv1/"
|
| 8 |
+
REGION_NAME = "eu-west-2"
|
| 9 |
+
BUCKET_NAME = "ogtl-analytics"
|
| 10 |
+
|
| 11 |
+
# Define unique codes for each folder
|
| 12 |
+
FOLDER_CODES = {
|
| 13 |
+
"flutterwave": "xwy3455",
|
| 14 |
+
"canvassing": "0thdkwn",
|
| 15 |
+
"test": "0thdkwn",
|
| 16 |
+
# Add more folders and codes here...
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Define subfolders for each folder
|
| 20 |
+
SUBFOLDERS = {
|
| 21 |
+
"flutterwave": ["teammate-performance", "product"],
|
| 22 |
+
"canvassing": ["agent-performance-vicidial"],
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Connect to S3 client
|
| 26 |
+
s3_client = boto3.client(
|
| 27 |
+
"s3",
|
| 28 |
+
aws_access_key_id=ACCESS_KEY,
|
| 29 |
+
aws_secret_access_key=SECRET_KEY,
|
| 30 |
+
region_name=REGION_NAME,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
def preview_data(input_data, folder, subfolder):
|
| 34 |
+
if folder=='canvassing' and subfolder=='agent-performance-vicidial':
|
| 35 |
+
return pd.read_csv(input_data,header=3, nrows=10)
|
| 36 |
+
|
| 37 |
+
elif folder=='flutterwave' and subfolder=='teammate-performance':
|
| 38 |
+
return pd.read_csv(input_data,header=3, nrows=10)
|
| 39 |
+
|
| 40 |
+
elif folder=='flutterwave' and subfolder=='product':
|
| 41 |
+
return pd.read_csv(input_data,low_memory=False, nrows=10)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# Get a list of folders in the bucket
|
| 45 |
+
folders = [obj["Key"].split("/")[0] for obj in s3_client.list_objects_v2(Bucket=BUCKET_NAME)["Contents"]]
|
| 46 |
+
|
| 47 |
+
# Streamlit app
|
| 48 |
+
st.title("Upload CSV File to S3 Bucket")
|
| 49 |
+
|
| 50 |
+
# Upload file
|
| 51 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type=".csv")
|
| 52 |
+
|
| 53 |
+
# Check if file is None or not a CSV file
|
| 54 |
+
if uploaded_file and uploaded_file.type != "text/csv":
|
| 55 |
+
st.error("Only CSV files are allowed!")
|
| 56 |
+
uploaded_file = None
|
| 57 |
+
|
| 58 |
+
# Show the rest of the app only if a valid CSV file is uploaded
|
| 59 |
+
if uploaded_file:
|
| 60 |
+
|
| 61 |
+
# Choose folder
|
| 62 |
+
selected_folder = st.selectbox("Select Campaign", set(folders))
|
| 63 |
+
|
| 64 |
+
# Check if folder has subfolders
|
| 65 |
+
if selected_folder in SUBFOLDERS:
|
| 66 |
+
# Show subfolder selection
|
| 67 |
+
selected_subfolder = st.selectbox("Select folder", SUBFOLDERS[selected_folder])
|
| 68 |
+
else:
|
| 69 |
+
selected_subfolder = None
|
| 70 |
+
|
| 71 |
+
# Preview data
|
| 72 |
+
# Read a few rows of data
|
| 73 |
+
data = preview_data(uploaded_file, selected_folder, selected_subfolder)
|
| 74 |
+
|
| 75 |
+
# Show data preview
|
| 76 |
+
st.subheader("Data preview: Please Confirm the Data Before Proceeding")
|
| 77 |
+
st.write(data)
|
| 78 |
+
|
| 79 |
+
# Input unique code
|
| 80 |
+
unique_code = st.text_input("Enter your unique code")
|
| 81 |
+
|
| 82 |
+
# Upload button
|
| 83 |
+
upload_button = st.button("Upload")
|
| 84 |
+
|
| 85 |
+
if upload_button:
|
| 86 |
+
|
| 87 |
+
# Validate unique code
|
| 88 |
+
if FOLDER_CODES.get(selected_folder) != unique_code:
|
| 89 |
+
st.error("Invalid unique code for selected folder")
|
| 90 |
+
else:
|
| 91 |
+
# Read entire uploaded file
|
| 92 |
+
data = pd.read_csv(uploaded_file)
|
| 93 |
+
|
| 94 |
+
# Create filename based on folder and subfolder
|
| 95 |
+
if selected_subfolder:
|
| 96 |
+
filename = f"{selected_folder}/{selected_subfolder}/{uploaded_file.name}"
|
| 97 |
+
else:
|
| 98 |
+
filename = f"{selected_folder}/{uploaded_file.name}"
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
# Upload notification
|
| 102 |
+
st.info("Please wait, upload is in progress...")
|
| 103 |
+
|
| 104 |
+
# Upload file to S3
|
| 105 |
+
s3_client.put_object(Body=uploaded_file, Bucket=BUCKET_NAME, Key=filename)
|
| 106 |
+
st.empty()
|
| 107 |
+
st.success("File uploaded successfully!")
|
| 108 |
+
|
| 109 |
+
except:
|
| 110 |
+
st.write("An Error Occured")
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# Clear file input and unique code
|
| 115 |
+
uploaded_file = None
|
| 116 |
+
unique_code = ""
|
| 117 |
+
|
| 118 |
+
# # Choose folder
|
| 119 |
+
# selected_folder = st.selectbox("Select folder", folders)
|
| 120 |
+
#
|
| 121 |
+
# # Input unique code
|
| 122 |
+
# unique_code = st.text_input("Enter unique code")
|
| 123 |
+
#
|
| 124 |
+
# # Upload button
|
| 125 |
+
# upload_button = st.button("Upload")
|
| 126 |
+
#
|
| 127 |
+
# if upload_button:
|
| 128 |
+
# # Validate unique code
|
| 129 |
+
# if FOLDER_CODES.get(selected_folder) != unique_code:
|
| 130 |
+
# st.error("Invalid unique code for selected folder")
|
| 131 |
+
# else:
|
| 132 |
+
# # Read uploaded file
|
| 133 |
+
# data = pd.read_csv(uploaded_file)
|
| 134 |
+
#
|
| 135 |
+
# # Create filename
|
| 136 |
+
# filename = f"{selected_folder}/{uploaded_file.name}"
|
| 137 |
+
#
|
| 138 |
+
# # Upload file to S3
|
| 139 |
+
# s3_client.put_object(Body=data.to_csv(), Bucket=BUCKET_NAME, Key=filename)
|
| 140 |
+
#
|
| 141 |
+
# st.success("File uploaded successfully!")
|
| 142 |
+
#
|
| 143 |
+
# # Clear file input and unique code
|
| 144 |
+
# uploaded_file = None
|
| 145 |
+
# unique_code = ""
|
requirements.txt
ADDED
|
Binary file (1.79 kB). View file
|
|
|