1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| import boto3 from botocore.exceptions import NoCredentialsError, PartialCredentialsError import streamlit as st import pandas as pd
BUCKET_NAME = 'hankbucket'
def get_s3_client(): return boto3.client( 's3', endpoint_url='https://OOOOO.OO.synologyc2.net', aws_access_key_id='twmOPTcq8MAxXaNVMOOOOOOOOO', aws_secret_access_key='n7O1Pys3nyp15T5OGOOOOOOO' )
def list_files(bucket_name): s3 = get_s3_client() try: response = s3.list_objects_v2(Bucket=bucket_name) files = response.get('Contents', []) file_list = [] for file in files: file_info = { "File Name": file['Key'], "Size (bytes)": file['Size'], "Last Modified": file['LastModified'], "ETag": file['ETag'] } file_list.append(file_info) return file_list except (NoCredentialsError, PartialCredentialsError) as e: return str(e)
def upload_file(bucket_name, file_path, object_name): s3 = get_s3_client() try: with open(file_path, 'rb') as f: s3.upload_fileobj(f, bucket_name, object_name) return f"Uploaded {object_name} to {bucket_name}" except Exception as e: return str(e)
def upload_file_and_get_etag(bucket_name, file_path, object_name): s3 = get_s3_client() try: with open(file_path, 'rb') as f: response = s3.upload_fileobj(f, bucket_name, object_name) return response['ETag'] except Exception as e: return str(e)
def upload_new_file(): uploaded_file = st.file_uploader("Choose a file") if uploaded_file is not None: with open(uploaded_file.name, "wb") as f: f.write(uploaded_file.getbuffer()) etag = upload_file_and_get_etag(BUCKET_NAME, uploaded_file.name, uploaded_file.name) st.write(f"Uploaded {uploaded_file.name} with ETag: {etag}")
def delete_file(bucket_name, object_name): s3 = get_s3_client() try: s3.delete_object(Bucket=bucket_name, Key=object_name) return f"Deleted {object_name} from {bucket_name}" except Exception as e: return str(e)
def list_all_files(): files = list_files(BUCKET_NAME) if files: file_list = [{"File Name": file['File Name'], "Etag": file['ETag'], "Size (bytes)": file['Size (bytes)'], "Last Modified": file['Last Modified']} for file in files] df = pd.DataFrame(file_list) return df else: return pd.DataFrame(columns=["File Name", "ETag"])
def download_file(bucket_name, object_name): s3 = get_s3_client() try: with open(object_name, 'wb') as f: s3.download_fileobj(bucket_name, object_name, f) return f"Downloaded {object_name} from {bucket_name}" except Exception as e: return str(e)
def download_selected_file(selected_file): response = download_file(BUCKET_NAME, selected_file) st.write(response)
st.title("Synology C2 Storage Management with S3 API")
st.header("List and Download Files") files_df = list_all_files() st.dataframe(files_df)
if not files_df.empty: selected_file = st.selectbox("Select a file to download", files_df["File Name"].tolist()) if st.button("Download File"): download_selected_file(selected_file)
st.header("Upload a New File and Get Unique ETag") upload_new_file()
|