Dcook
Dcook

Reputation: 961

how to check if a folder in s3 bucket exists or not in shell script

I have a bucket named: bucket1. I have to check whether a specific folder which is equal to previous month exists or not.

So, for example I want to run the script from aws datapipeline today, at first it will check if the previous month folder in s3 exists or not using shellcommand activity.

I am using this but it's giving true for all cases.Can anyone please help in this?

#!/bin/bash

lastmonth='date -d "$(date +%Y-%m-1) -1 month" +%m'
newpath="s3://bucket1//2020/$lastmonth/"                     
echo "Path is : $newpath"

state='aws s3 ls $newpath'
if [ -z "$state" ]
then
     exit 1
else
    echo "Path exists"
    exit 0
fi

Upvotes: 1

Views: 3547

Answers (1)

Dcook
Dcook

Reputation: 961

#!/bin/bash

lastmonth=`date -d "$(date +%Y-%m-1) -1 month" +%m`
newpath="s3://bucket1//2020/$lastmonth/"                     
echo "Path is : $newpath"

state=`aws s3 ls $newpath`
if [ -z "$state" ]
then
     exit 1
else
    echo "Path exists"
    exit 0
fi

Upvotes: 4

Related Questions