Post

AWS S3 - Create folder or prefix if not exists

I’d like to create a top-level directory in S3 if that directory doesn’t exist.

Since S3 deals with objects, the concept of a directory is mapped to S3’s concept of prefix.

The AWS S3 Cli call returns a json response containing the key Contents if the prefix (aka folder) exists. Because the response is json, I rely on jq to check if the result contains the Contents key.

1
2
3
4
5
6
7
8
9
10
11
12
13
S3_BUCKET_NAME="mys3bucket"
TOP_LEVEL_DIR="helloDir"

list_obj=$(aws s3api list-objects --bucket "$S3_BUCKET_NAME" --prefix "$TOP_LEVEL_DIR")
contents=$(echo $list_obj | jq '.Contents | length')
echo $contents

if [[ $contents -eq 0 ]]; then
    echo "INFO: Top-level dir, $TOP_LEVEL_DIR not found. So creating one."
    aws s3api put-object --bucket "$S3_BUCKET_NAME" --key "$TOP_LEVEL_DIR"
else
    echo "INFO: Top-level dir, $TOP_LEVEL_DIR."
fi
This post is licensed under CC BY 4.0 by the author.