SaiyanGirl
SaiyanGirl

Reputation: 17034

Upload to aws s3 using cli without folder path

I'd like to upload a file.txt on aws s3 that is located in something like main/part1/part2/file.txt, where part1 and part2 are unknown (those folders always change). I can do that with the command aws s3 cp ./main s3://mybucket --exclude "*" --include "*.txt" --recursive, but then in my bucket I have the file located in part1/part2/file.txt. I'd like file.txt to be at the base of the bucket, not inside part1/part2

Is that possible given that part1 and part2 are constantly changing?

Upvotes: 0

Views: 876

Answers (2)

Harish KM
Harish KM

Reputation: 1373

for dir1 in $(ls main); do
    for dir2 in $(ls main/$dir1); do
        aws s3 cp ./main/$dir1/$dir2/ s3://my-bucket --exclude "*" --include "*.txt" --recursive
    done
done
upload: main/part1/part2/file.txt to s3://my-bucket/file.txt
upload: main/part11/part22/file2.txt to s3://my-bucket/file2.txt

Upvotes: 2

Harish KM
Harish KM

Reputation: 1373

The following will work if main will never contain more than 1 subdirectory at a time (part1) & that subdirectory in-turn will never contain more than 1 subdirectory at a time (part2):

aws s3 cp ./main/*/*/ s3://my-bucket --exclude "*" --include "*.txt" --recursive
upload: main/part1/part2/file.txt to s3://my-bucket/file.txt

Upvotes: 2

Related Questions