-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_upload.py
257 lines (209 loc) · 6.86 KB
/
aws_upload.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
import argparse
import csv
import logging
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import List, Set
import boto3
from botocore.exceptions import ClientError
# Set up logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.FileHandler("pdf_upload.log"), logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
def parse_csv_inventory(csv_path: str) -> tuple[List[Path], Set[int]]:
"""
Parse CSV inventory file and create a list of unique PDF paths.
Parameters
----------
csv_path : str
Path to the CSV inventory file.
Returns
-------
tuple[List[Path], Set[int]]
A tuple containing:
- List of Path objects for unique PDFs
- Set of integers representing processed accession numbers
Notes
-----
Skips duplicate accession numbers and logs them.
"""
upload_list = []
processed_accessions = set()
try:
with open(csv_path, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
pdf_path = Path(row["name"])
try:
accession_num = int(pdf_path.stem)
if accession_num in processed_accessions:
logger.warning(
"Duplicate accession number found: "
+ f"{accession_num} - {pdf_path}"
)
continue
processed_accessions.add(accession_num)
upload_list.append(pdf_path)
except ValueError:
logger.error(
f"Invalid accession number format: {pdf_path.stem}"
)
continue
except Exception as e:
logger.error(f"Error processing CSV file: {e}")
raise
return upload_list, processed_accessions
def get_s3_inventory(s3_uri: str) -> Set[str]:
"""
Get inventory of existing PDF files in S3 bucket.
Parameters
----------
s3_uri : str
S3 URI in format s3://bucket-name/prefix
Returns
-------
Set[str]
Set of PDF filenames (without path) that exist in S3
Notes
-----
Only includes files with .pdf extension (case insensitive)
"""
try:
bucket_name = s3_uri.split("/")[2]
prefix = "/".join(s3_uri.split("/")[3:])
s3_client = boto3.client("s3")
existing_files = set()
paginator = s3_client.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=bucket_name, Prefix=prefix):
if "Contents" in page:
for obj in page["Contents"]:
if obj["Key"].lower().endswith(".pdf"):
existing_files.add(Path(obj["Key"]).name)
return existing_files
except Exception as e:
logger.error(f"Error getting S3 inventory: {e}")
raise
def filter_upload_list(
upload_list: List[Path], s3_inventory: Set[str]
) -> List[Path]:
"""
Remove files that already exist in S3 from upload list.
Parameters
----------
upload_list : List[Path]
List of Path objects to potentially upload
s3_inventory : Set[str]
Set of filenames that already exist in S3
Returns
-------
List[Path]
Filtered list of Path objects to upload
"""
filtered_list = [
path for path in upload_list if path.name not in s3_inventory
]
skipped = len(upload_list) - len(filtered_list)
if skipped > 0:
logger.info(f"Skipping {skipped} files that already exist in S3")
return filtered_list
def upload_file(file_path: Path, s3_uri: str) -> bool:
"""
Upload a single file to S3.
Parameters
----------
file_path : Path
Path to file to upload
s3_uri : str
S3 URI destination
Returns
-------
bool
True if upload successful, False otherwise
"""
try:
bucket_name = s3_uri.split("/")[2]
prefix = "/".join(s3_uri.split("/")[3:])
s3_key = f"{prefix}/{file_path.name}"
s3_client = boto3.client("s3")
s3_client.upload_file(str(file_path), bucket_name, s3_key)
logger.info(f"Successfully uploaded: {file_path.name}")
return True
except ClientError as e:
logger.error(f"Error uploading {file_path.name}: {e}")
return False
def parallel_upload(file_list: List[Path], s3_uri: str, num_threads: int):
"""
Upload files to S3 in parallel using threading.
Parameters
----------
file_list : List[Path]
List of files to upload
s3_uri : str
S3 URI destination
num_threads : int
Number of upload threads to use
Notes
-----
Uses ThreadPoolExecutor for parallel uploads
"""
with ThreadPoolExecutor(max_workers=num_threads) as executor:
results = list(
executor.map(lambda x: upload_file(x, s3_uri), file_list)
)
success_count = sum(1 for r in results if r)
logger.info(
f"Upload complete. {success_count}/{len(file_list)} "
+ "files uploaded successfully"
)
def main():
"""
Main function to process command line arguments and orchestrate the upload
process.
"""
parser = argparse.ArgumentParser(
description="Process PDF inventory and upload to S3"
)
parser.add_argument(
"--inventory", required=True, help="Path to CSV inventory file"
)
parser.add_argument(
"--s3-uri",
required=True,
help="S3 URI destination (s3://bucket-name/prefix)",
)
parser.add_argument(
"--threads",
type=int,
default=6,
help="Number of upload threads (default: 6)",
)
args = parser.parse_args()
try:
# Parse inventory and get unique files
logger.info("Parsing CSV inventory...")
upload_list, processed_accessions = parse_csv_inventory(args.inventory)
logger.info(f"Found {len(upload_list)} unique PDFs to process")
# Get S3 inventory
logger.info("Getting S3 inventory...")
s3_inventory = get_s3_inventory(args.s3_uri)
logger.info(f"Found {len(s3_inventory)} existing PDFs in S3")
# Filter out existing files
filtered_list = filter_upload_list(upload_list, s3_inventory)
logger.info(f"{len(filtered_list)} files to upload")
# Perform parallel upload
if filtered_list:
logger.info(
f"Starting parallel upload with {args.threads} threads..."
)
parallel_upload(filtered_list, args.s3_uri, args.threads)
else:
logger.info("No files to upload")
except Exception as e:
logger.error(f"Error in main process: {e}")
raise
if __name__ == "__main__":
main()