Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if folder exists in s3 using aws cli?

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 .

like image 427
arpit joshi Avatar asked Dec 04 '22 18:12

arpit joshi


2 Answers

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

like image 152
RyanKim Avatar answered May 28 '23 12:05

RyanKim


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'
like image 28
bzumby Avatar answered May 28 '23 11:05

bzumby