UPD: Modified s3-restore-status.py to use csv library

This commit is contained in:
xpk
2025-11-11 19:18:30 +08:00
parent ae71b76fc6
commit 7bc9fd255c
+12 -16
View File
@@ -41,10 +41,11 @@ Example Output:
""" """
import boto3 import boto3
import csv
import sys import sys
def read_objectlist(path: str = "/tmp/objectlist.csv"): def read_objectlist(path: str = "/tmp/objectlist.csv") -> tuple[list[str], str]:
"""Read object list CSV file and extract bucket and object keys. """Read object list CSV file and extract bucket and object keys.
Parses a CSV file containing bucket and object key pairs. Each line should Parses a CSV file containing bucket and object key pairs. Each line should
@@ -52,6 +53,9 @@ def read_objectlist(path: str = "/tmp/objectlist.csv"):
keys and returns the last bucket name encountered (assuming all objects are keys and returns the last bucket name encountered (assuming all objects are
in the same bucket). in the same bucket).
Uses Python's csv module for proper CSV parsing, which handles edge cases
like quoted fields, escaped characters, and commas within fields.
Args: Args:
path (str): Path to the CSV file containing bucket and object key pairs. path (str): Path to the CSV file containing bucket and object key pairs.
Defaults to "/tmp/objectlist.csv". Defaults to "/tmp/objectlist.csv".
@@ -64,23 +68,15 @@ def read_objectlist(path: str = "/tmp/objectlist.csv"):
Raises: Raises:
SystemExit: If the file is not found, the script exits with status code 1. SystemExit: If the file is not found, the script exits with status code 1.
Note:
- Lines starting with '#' are treated as comments and ignored
- Empty lines are ignored
- Malformed lines (not containing exactly one comma) are silently skipped
- The function assumes all objects are in the same bucket (returns last bucket)
""" """
bucket_to_keys = [] bucket_to_keys: list[str] = []
bucket: str = ""
try: try:
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8", newline="") as f:
for raw_line in f: reader = csv.reader(f)
line = raw_line.strip() for row in reader:
# Split only on the first comma to allow commas in keys if ever present bucket = row[0].strip()
parts = line.split(",", 1) key = row[1].strip()
if len(parts) != 2:
continue
bucket = parts[0].strip()
key = parts[1].strip()
bucket_to_keys.append(key) bucket_to_keys.append(key)
except FileNotFoundError: except FileNotFoundError:
print(f"Error: object list file not found at {path}", file=sys.stderr) print(f"Error: object list file not found at {path}", file=sys.stderr)