1111"""
1212
1313import argparse
14- import sys
14+ import json
15+ import logging
1516import os
17+ import sys
1618import time
19+ from datetime import datetime , timezone
20+
1721import boto3
18- from botocore .exceptions import ClientError , BotoCoreError
19- from influxdb_client_3 import InfluxDBClient3 , WritePrecision
20- import json
21- import logging
2222import requests
23- from datetime import datetime , timezone
23+ from botocore .exceptions import BotoCoreError , ClientError
24+ from influxdb_client_3 import InfluxDBClient3 , WritePrecision
2425
2526MIGRATION_METADATA_TABLE : str = "liveanalytics_migration_metadata"
2627TRIGGER_NAME : str = "migration_trigger"
@@ -38,6 +39,7 @@ def __init__(
3839 timeout_seconds : int = 360 , # 6 minutes.
3940 region : str = "us-west-2" ,
4041 max_parquet_files : int | None = None ,
42+ presigned_url_expiry_seconds : int | None = None ,
4143 ) -> None :
4244 """
4345 Initialize
@@ -49,6 +51,7 @@ def __init__(
4951 timeout_seconds (int): The number of seconds to wait for each migration request.
5052 region (str): The AWS Region to use.
5153 max_parquet_files (int | None): The maximum number of Parquet files to migrate.
54+ presigned_url_expiry_seconds (int | None): The expiry time for all presigned URLs.
5255
5356 Returns:
5457 None
@@ -59,6 +62,7 @@ def __init__(
5962 self .timeout_seconds : int = timeout_seconds
6063 self .region : str = region
6164 self .max_parquet_files : int | None = max_parquet_files
65+ self .presigned_url_expiry_seconds : int | None = presigned_url_expiry_seconds
6266
6367 logging .basicConfig (
6468 level = logging .INFO , format = "%(asctime)s - %(levelname)s - %(message)s"
@@ -582,7 +586,7 @@ def get_s3_objects_list(
582586 return list (files_to_migrate )
583587
584588 def generate_metadata (
585- self , parquet_file_names : list [str ], expiration : int = 604_800
589+ self , parquet_file_names : list [str ], expiration : int | None = 604_800
586590 ) -> dict [str , dict [str , str ]]:
587591 """
588592 Generates metadata, including presigned URLs for S3 objects.
@@ -675,7 +679,7 @@ def setup_influxdb_metadata(self) -> bool:
675679 )
676680 return False
677681 except requests .exceptions .RequestException as e :
678- self .error (f "Error creating database: " , str (e ))
682+ self .error ("Error creating database: " , str (e ))
679683 return False
680684
681685 self .delete_metadata_table (silence_errors = True )
@@ -707,12 +711,12 @@ def setup_influxdb_metadata(self) -> bool:
707711 )
708712 return False
709713 except requests .exceptions .RequestException as e :
710- self .error (f "Error creating table: " , str (e ))
714+ self .error ("Error creating table: " , str (e ))
711715 return False
712716 return True
713717
714718 except Exception as e :
715- self .error (f "Failed to setup InfluxDB metadata: " , str (e ))
719+ self .error ("Failed to setup InfluxDB metadata: " , str (e ))
716720 sys .exit (1 )
717721
718722 def write_metadata_to_influxdb (self , metadata ):
@@ -783,7 +787,7 @@ def write_metadata_to_influxdb(self, metadata):
783787 )
784788
785789 except Exception as e :
786- self .error (f "Failed to write metadata to InfluxDB: " , str (e ))
790+ self .error ("Failed to write metadata to InfluxDB: " , str (e ))
787791 sys .exit (1 )
788792
789793 def bulk_invoke_http_trigger (self , metadata ):
@@ -1012,7 +1016,7 @@ def create_processing_engine_trigger(self):
10121016 None
10131017 """
10141018 try :
1015- self .info (f "Creating processing engine HTTP trigger" )
1019+ self .info ("Creating processing engine HTTP trigger" )
10161020
10171021 headers = {
10181022 "Authorization" : f"Bearer { self .influx_token } " ,
@@ -1081,7 +1085,8 @@ def run(self):
10811085 parquet_file_names : list [str ] = self .get_s3_objects_list ()
10821086
10831087 metadata : dict [str , dict [str , str ]] = self .generate_metadata (
1084- parquet_file_names = parquet_file_names
1088+ parquet_file_names = parquet_file_names ,
1089+ expiration = self .presigned_url_expiry_seconds ,
10851090 )
10861091
10871092 # Setup InfluxDB metadata.
@@ -1160,7 +1165,7 @@ def delete_unloaded_data(self):
11601165 Key = marker ["Key" ],
11611166 VersionId = marker ["VersionId" ],
11621167 )
1163- except Exception as e :
1168+ except Exception :
11641169 self .error (
11651170 f"Failed to delete deletion marker for object { marker ['Key' ]} with version ID { marker ['VersionId' ]} "
11661171 )
@@ -1417,6 +1422,12 @@ def main(input_args) -> int:
14171422 type = int ,
14181423 help = "Optional. The maximum number of Parquet files to migrate. Default behaviour is to migrate all Parquet files." ,
14191424 )
1425+ parser .add_argument (
1426+ "--presigned-url-expiry-seconds" ,
1427+ required = False ,
1428+ type = int ,
1429+ help = "Optional. The number of seconds that all presigned URLs will be valid for. Default is 604800 (7 days, max)." ,
1430+ )
14201431
14211432 args = parser .parse_args (input_args )
14221433
@@ -1426,6 +1437,7 @@ def main(input_args) -> int:
14261437 timeout_seconds : int = args .timeout_seconds
14271438 region : str = args .region
14281439 max_parquet_files : int | None = args .max_parquet_files
1440+ presigned_url_expiry_seconds : int | None = args .presigned_url_expiry_seconds
14291441
14301442 required_env_vars = [
14311443 "INFLUXDB3_HOST_URL" ,
@@ -1448,6 +1460,7 @@ def main(input_args) -> int:
14481460 timeout_seconds = timeout_seconds ,
14491461 region = region ,
14501462 max_parquet_files = max_parquet_files ,
1463+ presigned_url_expiry_seconds = presigned_url_expiry_seconds ,
14511464 )
14521465
14531466 try :
0 commit comments