iot-s3-data / app.py
verma007's picture
Upload 2 files
f1a74cb verified
import streamlit as st
import boto3
import os
from botocore.exceptions import NoCredentialsError
def upload_file_to_s3(file, bucket, object_name=None):
s3_client = boto3.client('s3')
try:
# Saving the file temporarily
temp_file = f"temp_{file.name}"
with open(temp_file, "wb") as f:
f.write(file.getbuffer())
# Upload to S3
response = s3_client.upload_file(temp_file, bucket, object_name or file.name)
st.success(f"Image uploaded: {file.name}")
# Clean up: delete the temporary file
os.remove(temp_file)
except NoCredentialsError as e:
st.error('Credentials not available')
return False
except Exception as e:
st.error(f"An error occurred: {e}")
return False
return True
def main():
st.title('Image Upload to AWS S3')
uploaded_file = st.file_uploader("Choose an image...", type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
# Display the image
st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)
# Upload button
if st.button('Upload to S3'):
bucket_name = "iot-ultra-sonic-data" # Replace with your bucket name
upload_file_to_s3(uploaded_file, bucket_name)
if __name__ == "__main__":
main()