To build a desktop app using Python we need Tkinter module and to store images into AWS S3, you'll need to use the Boto3 library for AWS interaction. Here's an example of how you can achieve this:
Install the required libraries:
pip install boto3
.
import os
from tkinter import *
from tkinter import filedialog
import boto3
from botocore.exceptions import NoCredentialsError
import threading
from tkinter import ttk
3. Create a Tkinter window:
root = Tk()
root.title("Image Uploader")
4. Create a function to handle the image upload:
def upload_image():
# Open file dialog to select an image
file_path = filedialog.askopenfilename()
if file_path:
# Connect to AWS S3
s3 = boto3.client('s3',
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
# Extract file name from the path
file_name = file_path.split('/')[-1]
# Get total file size in bytes
total_bytes = os.path.getsize(file_path)
# Create a progress bar
progress_bar = ttk.Progressbar(root, length=200, mode='determinate')
progress_bar.pack(pady=10)
# Function to update the progress bar
def update_progress(bytes_amount):
progress = (bytes_amount / total_bytes) * 100
progress_bar['value'] = progress
root.update_idletasks()
# Function to upload the image
def upload():
try:
s3.upload_file(file_path, BUCKET_NAME, file_name, ExtraArgs={'ACL': 'public-read'},
Callback=update_progress)
# Display success message
message_label.configure(text="Image uploaded successfully!")
except NoCredentialsError:
message_label.configure(text="AWS credentials not found!")
finally:
# Remove the progress bar
progress_bar.destroy()
# Start a new thread for uploading
upload_thread = threading.Thread(target=upload)
upload_thread.start()
else:
# Display error message if no file was selected
message_label.configure(text="No image selected!")
5.Create a button to trigger the image upload function:
upload_button = Button(root, text="Upload Image", command=upload_image)
upload_button.pack(pady=10)
Make sure to replace 'YOUR_ACCESS_KEY'
, 'YOUR_SECRET_ACCESS_KEY'
, and 'YOUR_BUCKET_NAME'
with your AWS credentials and bucket name.
import os
from tkinter import *
from tkinter import filedialog
import boto3
from botocore.exceptions import NoCredentialsError
import threading
from tkinter import ttk
# AWS S3 credentials
AWS_ACCESS_KEY = 'YOUR_ACCESS_KEY'
AWS_SECRET_ACCESS_KEY = 'YOUR_SECRET_ACCESS_KEY'
BUCKET_NAME = 'YOUR_BUCKET_NAME'
def upload_image():
# Open file dialog to select an image
file_path = filedialog.askopenfilename()
if file_path:
# Connect to AWS S3
s3 = boto3.client('s3',
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
# Extract file name from the path
file_name = file_path.split('/')[-1]
# Get total file size in bytes
total_bytes = os.path.getsize(file_path)
# Create a progress bar
progress_bar = ttk.Progressbar(root, length=200, mode='determinate')
progress_bar.pack(pady=10)
# Function to update the progress bar
def update_progress(bytes_amount):
progress = (bytes_amount / total_bytes) * 100
progress_bar['value'] = progress
root.update_idletasks()
# Function to upload the image
def upload():
try:
s3.upload_file(file_path, BUCKET_NAME, file_name, ExtraArgs={'ACL': 'public-read'},
Callback=update_progress)
# Display success message
message_label.configure(text="Image uploaded successfully!")
except NoCredentialsError:
message_label.configure(text="AWS credentials not found!")
finally:
# Remove the progress bar
progress_bar.destroy()
# Start a new thread for uploading
upload_thread = threading.Thread(target=upload)
upload_thread.start()
else:
# Display error message if no file was selected
message_label.configure(text="No image selected!")
root = Tk()
root.title("Image Uploader")
upload_button = Button(root, text="Upload Image", command=upload_image)
upload_button.pack(pady=10)
message_label = Label(root, text="")
message_label.pack()
root.mainloop()