I am trying to write a code wherein in the S3 bucket I want to check if the folder exists and if not, I want to create the folder. Following is my code.
def checkiffolderexists(bucket:str, path:str) -> bool:
s3 = boto3.Session(profile_name='saml').client('s3')
if not path.endswith('/'):
path = path '/'
print(path)
resp = s3.list_objects(Bucket=bucket, Prefix=path, Delimiter='/', MaxKeys=1)
return 'Contents' in resp
I am passing the following arguments to this method created.
checkiffolderexists('star-mi-qa-ctset-delta-us-east-1','star-mi-qa-ctset-delta-us-east-1/vendor=ctset/type=own_v5/year=2022/month=10/day=01')
star-mi-qa-ctset-delta-us-east-1 is my bucket name and inside that I want to check if day=01/ folder is present or not. If not, I want to create that folder in the same path which I have passed to the method. The problem here is even if the folder exists there, the method is returning me false. Any mistake I am doing while passing the arguments to the method or in the code?
CodePudding user response:
Keys that match the Delimiter will not show up in the Contents
.
From the documentation:
Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response.
(Emphasis mine.)
Calling the method without this parameter will work:
resp = s3.list_objects(Bucket=bucket, Prefix=path, MaxKeys=1)