Rajib Biswas
Rajib Biswas

Reputation: 882

Copy files from AWS S3 Bucket only if the bucket exists

I am trying to create a single command through AWS CLI, which will check if the provided bucket exists and if there are files present inside the bucket, then copy those files in local machine.

So my requirement is: there is a s3 bucket 'ecs-ref-arch' and this bucket contains a folder called 'jars'. So if /ecs-ref-arch/jars is present and there are files inside this directory, then only I want to copy all files from jars directory to my local machine.

Can this be done in single AWS CLI command?

Upvotes: 0

Views: 1006

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53703

Can this be done in single AWS CLI command ?

First, it sounds a lot like 'do the job for me', you did not show a lot of research from your side, although you might have searched, its not clear where you struggle.

On the single command I am not sure, I am a big fan of xargs and so on, but I think you need at least 2 commands. AWS provides some methods to check existence of a bucket (aws s3api head-bucket --bucket <bucket_name>) but its not convenient to search for a folder

so the best is to check this in script and run all commands from there, such script could be written as

#!/bin/bash

CMD="aws s3 ls s3://<bucket-name>"

if eval "$CMD" | grep -q 'PRE jars/'; then
  # jars folder is found, can copy files locally
  aws s3 sync s3://<bucket-name>/jars .
fi

Upvotes: 1

Related Questions