Let's say I have a bucket named Test which has folder Alpha/TestingOne,Alpha/TestingTwo . I want to check if a folder named Alpha/TestingThree is present in my bucket using aws cli . I did try aws
s3api head-object --bucket Test --key Alpha/TestingThree
But seems the head-object is for files and not for folders . So is there a way to check if a folder exists in aws s3 using aws cli api .
Using aws cli,
aws s3 ls s3://Test/Alpha/TestingThree
If It is exists, it shows like below, else returns nothting.
PRE TestingThree
Note that S3 is flat structure (acutually no hierarchy like directory). https://docs.aws.amazon.com/AmazonS3/latest/user-guide/using-folders.html
There seem to be no way to test if a 'folder' exists in s3 bucket. That should be somehow related to the fact that everything is an 'object' with a key/value.
As an option, you could utilize list-objects-v2 or list-objects to check for the 'contents' of a prefix (aka 'folder'):
$ aws s3api list-objects-v2 --bucket <bucket_name> --prefix <non_exist_prefix_name> --query 'Contents[]'
null
Prefix (aka 'folder') that doesn't exist will always return 'null' for such query. Then you can use comparison against 'null' value:
$ test "$result" == "null" && echo 'yes' || echo 'no'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With