From 019a204d4759ef608c1c1f1daf72c0767bbaba85 Mon Sep 17 00:00:00 2001
From: Aymane HAFIANE <ayhafian@esa-maap.org>
Date: Fri, 2 Jun 2023 16:50:31 +0200
Subject: [PATCH] BIOMASS-2978 fix delete function: return 404 error when
 object not found & confirm delete filewhen found

---
 maap-s3.py | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/maap-s3.py b/maap-s3.py
index 75646c3..f487dbe 100755
--- a/maap-s3.py
+++ b/maap-s3.py
@@ -408,13 +408,26 @@ def delete(destination):
             userinfo = json.load(json_file)
             #Get the info
             token=userinfo['token']
-            
+
+        url = "https://gravitee-gateway."+MAAP_ENV_TYPE.lower()+".esa-maap.org/s3/"+destination
+        list_object = requests.get(url+'?list=true', headers = {'Authorization': 'Bearer '+token}, allow_redirects=False)
+        if list_object.status_code == 404:
+            print(list_object.text)
+            print('Object not found')
+            sys.exit(0)
+        elif list_object.status_code == 200:
+            user_confirm = input("type y/Y and press enter to confirm deletion of the file : " + destination+" : ")
+            if user_confirm not in ['y','Y']:
+                print('Abort')
+                sys.exit(0)
+
+
         #call the api to delete the data
         #Get the presigned url to delete the data
         url = "http://gravitee-gateway."+MAAP_ENV_TYPE.lower()+".esa-maap.org/s3/"+destination
         print(url)
-        response = requests.delete(url, headers = {'Authorization': 'Bearer '+token}, allow_redirects=False)    
-        
+        response = requests.delete(url, headers={'Authorization': 'Bearer ' + token}, allow_redirects=False)
+
         location = response.headers['Location']
         
         #We have the 
-- 
GitLab