I would like to check if a file exists in a separate directory of the bucket if a given file exists. I have the following directory structure-
import boto3
s3 = boto3.resource('s3')
def file_exists(fileN):
try:
s3.Object('my-bucket', 'folder1/folder2/'+fileN).load()
except:
return False
else:
fileN = fileN.split(".")[0]
try:
s3.Object('my-bucket', 'folder1/<randomid folderxxxx>/'+fileN+'_condition.jpg').load()
except:
return False
else:
return True
file_exists("test.jpg")
This works but as long as I can send the randomfolderID
as an argument. Is there a better and elegant way to do it?
Basically I have to check if,
my-bucket/folder1/folder2/test.jpg
if this exists then check
my-bucket/folder1/<randomID>/test_condition.jpg
if this also exists then return True