From 3ebc6d5dbac3a38bbc27b45875068678b48dadbe Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Mon, 5 Aug 2019 11:00:20 +0300
Subject: [PATCH] Fix a bug that botocore doesn't return a `Contents` key when
 the bucket is empty.

---
 check_s3_bucket/__init__.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/check_s3_bucket/__init__.py b/check_s3_bucket/__init__.py
index 4921461..751d640 100755
--- a/check_s3_bucket/__init__.py
+++ b/check_s3_bucket/__init__.py
@@ -31,7 +31,10 @@ def get_file_list(conn, bucket, prefix=""):
     # I'm not concerened with the limitation of number of keys in the
     # response as the buckets have a lifecycle rule enabled and files are
     # automatically moved of the bucket.
-    files = conn.list_objects_v2(Bucket=bucket, Prefix=prefix)["Contents"]
+    response = conn.list_objects_v2(Bucket=bucket, Prefix=prefix)
+    if "Contents" not in response:
+        return []
+    files = response["Contents"]
     files.sort(key=lambda x: x["LastModified"], reverse=True)
     files = files[:2]
     for file in files:
@@ -96,7 +99,7 @@ def main():
         exit(3)
 
     if not files:
-        print("Not matching files in bucket.")
+        print("No matching files in bucket.")
         exit(2)
 
     # Calculate the age of the latest file and if it's in the thresholds set.
-- 
GitLab