-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_from_s3.py.jinja
More file actions
55 lines (45 loc) · 1.93 KB
/
fetch_from_s3.py.jinja
File metadata and controls
55 lines (45 loc) · 1.93 KB
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
import boto3
from pathlib import Path
from {{module_name_slug}} import const
import tempfile
import tarfile
import shutil
import os
bucket_name = 'adu-project-data'
s3_path = '{{module_name}}/store_compressed.tar.gz'
s3 = boto3.client('s3')
def get_folder_size(folder_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size / (1024 * 1024) # Convert bytes to MB
if os.path.exists(const.store_path):
folder_size = get_folder_size(const.store_path)
user_input = input(f"The directory {const.store_path} already exists and is {folder_size:.2f} MB. Do you really want to fetch and overwrite it? (yes/no): ")
if user_input.lower() != 'yes':
print("Fetch operation aborted.")
exit()
# Get the ETag of the uploaded file and save it
s3 = boto3.client('s3')
response = s3.head_object(Bucket=bucket_name, Key=s3_path)
remote_etag = response['ETag'].strip('"') # Remove the quotes from the ETag
local_etag_path = Path('store_compressed_etag.txt')
local_etag = local_etag_path.read_text() if local_etag_path.exists() else None
if remote_etag == local_etag:
print("The directory is already up to date.")
exit()
# Create a temporary file to store the downloaded tar.gz archive
with tempfile.NamedTemporaryFile(delete=False, suffix='.tar.gz') as temp_file:
temp_file_path = temp_file.name
# Download the tar.gz file from S3
s3.download_file(bucket_name, s3_path, temp_file_path)
# Remove the existing store directory
if os.path.exists(const.store_path):
shutil.rmtree(const.store_path)
# Extract the tar.gz archive to const.store_path
with tarfile.open(temp_file_path, "r:gz") as tar:
tar.extractall(path=const.root_path)
# Save the ETag of the downloaded file
with open('store_compressed_etag.txt', 'w') as f: f.write(remote_etag)