88import sys
99import glob
1010
11+ SCRIPT_DIR = pathlib .Path (__file__ ).resolve ().parent
12+ BASE_DIR = SCRIPT_DIR .parent # project root (website files live here)
13+
1114# Set up region, domain name, bucket name, stack name, cloudformation file, and boto clients
1215region = 'us-east-1'
1316domain_name = 'derekmelder.com'
1417stack_name = 'derekmelder-website-stack'
1518bucket_name = 'www.' + domain_name
16- cloudformation_file = 'website.yml'
19+ cloudformation_file = BASE_DIR / 'website.yml'
1720
1821s3_client = boto3 .client ('s3' , region_name = region )
1922cloudformation_client = boto3 .client ('cloudformation' , region_name = region )
@@ -23,6 +26,8 @@ class MyStr(str):
2326
2427def upload_files (file_array ):
2528 for file in file_array :
29+ file_path = pathlib .Path (file ).resolve ()
30+ s3_key = file_path .relative_to (BASE_DIR ).as_posix ()
2631 content_type = ''
2732 match MyStr (file ):
2833 case '.doc' :
@@ -31,13 +36,14 @@ def upload_files(file_array):
3136 content_type = 'application/pdf'
3237 case '.html' :
3338 content_type = 'text/html'
34- s3_client .upload_file (Filename = file , Key = file , Bucket = bucket_name , ExtraArgs = {'ContentType' : content_type })
39+ s3_client .upload_file (Filename = file , Key = s3_key , Bucket = bucket_name , ExtraArgs = {'ContentType' : content_type })
3540
3641# Handle uploading files in directories and subdirectories
3742def upload_files_from_dir (path ):
38- for subdir , dirs , files in os .walk (path ):
43+ for subdir , dirs , files in os .walk (BASE_DIR / path ):
3944 for file in files :
4045 full_file_path = os .path .join (subdir , file )
46+ full_file_path = pathlib .Path (subdir ) / file
4147 full_file_path = pathlib .PureWindowsPath (full_file_path ).as_posix ()
4248 content_type = ''
4349 match MyStr (full_file_path ):
@@ -143,6 +149,7 @@ def upload_files_from_dir(path):
143149root_files = []
144150for files in types :
145151 root_files .extend (glob .glob (files ))
152+ root_files .extend (glob .glob (str (BASE_DIR / files )))
146153upload_files (root_files )
147154
148155# Upload files in subdirectories
@@ -154,4 +161,5 @@ def upload_files_from_dir(path):
154161upload_files_from_dir ('isotope' )
155162upload_files_from_dir ('js' )
156163upload_files_from_dir ('media' )
164+ upload_files_from_dir ('py' )
157165upload_files_from_dir ('video' )
0 commit comments