'copy data from s3 to local with prefix
I am trying to copy data from s3 to local with prefix using aws-cli.
But I am getting error with different regex.
aws s3 cp s3://my-bucket-name/RAW_TIMESTAMP_0506* . --profile prod
error:
no matches found: s3://my-bucket-name/RAW_TIMESTAMP_0506*
Solution 1:[1]
aws s3 cp s3://my-bucket/ <local directory path> --recursive --exclude "*" --include "<prefix>*"
This will copy only files with given prefix
Solution 2:[2]
The above answers to not work properly... for example I have many thousands of files in a directory by date, and I wish to retrieve only the files that are needed.. so I tried the correct version per the documents:
aws s3 cp s3://mybucket/sub /my/local/ --recursive --exclude "*" --include "20170906*.png"
and it did not download the prefixed files, but began to download everything
so then I tried the sample above:
aws s3 cp s3://mybucket/sub/ . /my/local --recursive --include "20170906*"
and it also downloaded everything... It seems that this is an ongoing issue with aws cli, and they have no intention to fix it... Here are some workarounds that I found while Googling, but they are less than ideal.
Solution 3:[3]
If you don't like silent consoles, you can pipe aws ls thru awk and back to aws cp.
Example
# url must be the entire prefix that includes folders.
# Ex.: url='s3://my-bucket-name/folderA/folderB',
# not url='s3://my-bucket-name'
url='s3://my-bucket-name/folderA/folderB'
prefix='RAW_TIMESTAMP_0506'
aws s3 ls "$url/$prefix" | awk '{system("aws s3 cp '"$url"'/"$4 " .")}'
Explanation
- The
lspart is pretty simple. I'm using variables to simplify and shorten the command. Always wrap shell variables in double quotes to prevent disaster. awk {print $4}would extract only the filenames from thelsoutput (NOT the S3 Key! This is why url must be the entire prefix that includes folders.)awk {system("echo " $4")}would do the same thing, but it accomplishes this by calling another command. Note: I did NOT use a subshell$(...), because that would run the entirels | awkpart before startingcp. That would be slow, and it wouldn't print anything for a looong time.awk '{system("echo aws s3 cp "$4 " .")}'would print commands that are very close to the ones we want. Pay attention to the spacing. If you try to run this, you'll notice something isn't quite right. This would produce commands likeaws s3 cp RAW_TIMESTAMP_05060402_whatever.log .awk '{system("echo aws s3 cp '$url'/"$4 " .")}'is what we're looking for. This adds the path to the filename. Look closely at the quotes. Remember we wrapped theawkparameter in single quotes, so we have to close and reopen the quotes if we want to use a shell variable in that parameter.awk '{system("aws s3 cp '"$url"'/"$4 " .")}'is the final version. We just removeechoto actually execute the commands created byawk. Of course, I've also surrounded the$urlvariable with double quotes, because it's good practice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eyshika |
| Solution 2 | Patrick Francis |
| Solution 3 | Zachary Ryan Smith |
