5
5
6
6
7
7
def is_bucket_accessible (bucket_name ):
8
- s3 = boto3 .client ('s3' )
8
+ s3 = boto3 .client ("s3" )
9
9
try :
10
10
s3 .head_bucket (Bucket = bucket_name )
11
11
return True
12
12
except ClientError as e :
13
- error_code = int (e .response [' Error' ][ ' Code' ])
13
+ error_code = int (e .response [" Error" ][ " Code" ])
14
14
if error_code == 403 :
15
15
print (f"Bucket { bucket_name } exists, but you don't have permission to access it." )
16
16
elif error_code == 404 :
@@ -19,11 +19,12 @@ def is_bucket_accessible(bucket_name):
19
19
print (f"Error checking bucket { bucket_name } : { e } " )
20
20
return False
21
21
22
+
22
23
def validate_s3_references (file_path ):
23
- with open (file_path , 'r' ) as file :
24
+ with open (file_path , "r" ) as file :
24
25
content = file .read ()
25
26
26
- s3_pattern = re .compile (r' s3:\/\/([a-zA-Z0-9._-]+)' )
27
+ s3_pattern = re .compile (r" s3:\/\/([a-zA-Z0-9._-]+)" )
27
28
matches = s3_pattern .findall (content )
28
29
29
30
invalid_buckets = []
@@ -33,8 +34,10 @@ def validate_s3_references(file_path):
33
34
34
35
return invalid_buckets
35
36
37
+
36
38
if __name__ == "__main__" :
37
39
import sys
40
+
38
41
if len (sys .argv ) < 2 :
39
42
print ("Usage: python s3_bucket_validator.py <file_path>" )
40
43
sys .exit (1 )
@@ -46,4 +49,4 @@ def validate_s3_references(file_path):
46
49
print (f"Invalid or inaccessible S3 buckets found: { ', ' .join (invalid_buckets )} " )
47
50
sys .exit (1 )
48
51
else :
49
- print ("All referenced S3 buckets are valid and accessible." )
52
+ print ("All referenced S3 buckets are valid and accessible." )
0 commit comments