Skip to content

Commit

Permalink
Merge pull request #89 from WorldCereal/develop
Browse files Browse the repository at this point in the history
 Merge branch 'develop' into 'main'
  • Loading branch information
mbattude-cs authored Apr 12, 2022
2 parents 6ec57fc + fe188f8 commit a28b5f8
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/ewoc_dag/bucket/eobucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,62 @@ def _check_bucket(self) -> bool:

return True

def _check_product_file(self, prefix) -> bool:
"""Check if the product contains a given file
Returns:
bool: return True if the product file is accessible and False otherwise
"""
try:
self._s3_client.head_object(Bucket=self._bucket_name, Key=prefix)
except ClientError as err:
error_code = err.response["Error"]["Code"]
if error_code == "404":
logger.critical("Path %s/%s does not exist!", self._bucket_name, prefix)
elif error_code == "403":
logger.critical("Acces forbidden to %s/%s path!", self._bucket_name, prefix)
return False

return True

def _check_product(self, prefix, threshold, request_payer: bool = False) -> bool:
"""Check if the product is usable
Returns:
bool: return True if the product is accessible and False otherwise
"""
if request_payer is True:
s3_result = self._s3_client.list_objects_v2(
Bucket=self._bucket_name,
Prefix=prefix,
Delimiter = "/",
RequestPayer="requester"
)
else:
s3_result = self._s3_client.list_objects_v2(
Bucket=self._bucket_name,
Prefix=prefix,
Delimiter = "/"
)

if 'Contents' not in s3_result:
logger.critical("Path %s/%s does not exist!", self._bucket_name, prefix)
return False
else:

list_product_files = []
for obj in s3_result.get('Contents'):
list_product_files.append(obj.get('Key'))

if len(list_product_files)>threshold:
logger.debug("Path %s/%s is full with %s files \n", \
self._bucket_name, prefix, len(list_product_files))
return True
else:
logger.debug("Path %s/%s is partial with %s files \n", \
self._bucket_name, prefix, len(list_product_files))
return False

def _s3_basepath(self) -> str:
"""Compute the basepath of the bucket s3://bucket_name
Expand All @@ -126,7 +182,7 @@ def _find_product(self, prd_path: str, prd_date: str) -> str:
Args:
prd_path (str): prd path
prd_date (str): prd date
Returns:
str: product name
"""
Expand All @@ -139,7 +195,7 @@ def list_folders(s3_client, bucket_name, prefix):
folder_list = list_folders(self._s3_client, self._bucket_name, prefix=prd_path)
for folder in folder_list:
logger.debug('Folder found: %s', folder.split('/')[-2])

if prd_date in folder.split('/')[-2]:
prd_name = folder.split('/')[-2]
logger.debug('Product name: %s', prd_name)
Expand Down

0 comments on commit a28b5f8

Please sign in to comment.