Use AWS CLI to Copy from S3 to EC2 - amazon-web-services

I have zipped files in an S3 bucket that I need to bring back to my EC2 instance. In the past, I moved the documents to S3 with the following command:
aws s3 cp /my/ec2/path/ s3://my/s3/path/ --exclude '*' --include '2014-01*’ —-recursive
To move files from January 2014 back to EC2, I have tried the following command:
aws s3 cp s3://my/s3/path/ //my/ec2/path/ --exclude '*' --include '2014-01*' --recursive
My understanding is that this command excludes all files but then includes all files with the prefix '2014-01'. I have confirmed that this is how the files I want start. I have also tried only one forward slash before mainstorage and including fewer files.
I have followed these two links from Amazon:
http://docs.aws.amazon.com/cli/latest/reference/s3/index.html
http://docs.aws.amazon.com/cli/latest/userguide/using-s3-commands.html

Figured it out. The key was to define the filepath in --include , i.e. --include '2014-1'. Correct command:
aws s3 cp s3://my/s3/path //my/ec2/path/ --exclude '*' --include '*2014-01*' --recursive

Related

Exclude macOS hidden files from AWS S3 sync

I'm syncing the entire contents of an external hard drive, used with macOS, to an S3 bucket. I'd like to exclude all macOS hidden files.
I've tried:
aws s3 sync --dryrun --exclude "^\." --exclude "\/\." ./ s3://bucketname
However, the result when I run that is exactly the same as just:
aws s3 sync --dryrun . s3://bucketname
So, I must be doing something wrong.
Any suggestions?
Thanks.
aws s3 sync --dryrun . s3://bucketname --exclude ".*" --exclude "*/.*"
Adding two exclusion arguments will hide both the specified files in the current directory as well as any in subfolders.
This seems to work:
aws s3 sync --dryrun . s3://bucketname --exclude ".*"
However, I don't think it will exclude such files in sub-directories.
Try this:
aws s3 sync --dryrun --exclude '*/.*'
This should remove any hidden files, including in subfolders.
aws s3 sync --recursive --dryrun --exclude '/.'

AWS CLI search a file in s3 bucket and copy to different folder

I am trying to copy only files from AWS S3 Folder_Test1 folder to a Folder_Test2 folder in the same bucket.
Folder_Test1:
T1_abc_june21.csv
T1_abc_june25.csv
T2_abc_june29.csv
T1_abc_def_june21.csv
T2_abc_def_june25.csv
T3_abc_def_june29.csv
T3_xyz_june29.csv
I have to filter the file name having only abc and exclude the files abc_def:
I tried:
aws s3 cp s3://$bucket/Folder_Test1/ s3://$bucket/Folder_Test2/ --exclude "*abc_def*" --include "*abc*"
but it is not working.
From s3 — AWS CLI 1.18.123 Command Reference:
Any number of these parameters can be passed to a command. You can do this by providing an --exclude or --include argument multiple times, e.g. --include ".txt" --include ".png". When there are multiple filters, the rule is the filters that appear later in the command take precedence over filters that appear earlier in the command.
Therefore, the problem is that your command is excluding *abc_def* but is then including *abc*, which adds the *abc_def* files again.
You should be able to fix it by swapping the order:
aws s3 cp s3://$bucket/Folder_Test1/ s3://$bucket/Folder_Test2/ --include "*abc*" --exclude "*abc_def*"
If it is copying other files that you do not want (eg xyz), then add an exclude:
aws s3 cp s3://$bucket/Folder_Test1/ s3://$bucket/Folder_Test2/ --exclude "*" --include "*abc*" --exclude "*abc_def*"
This will apply these rules in order:
Exclude everything
Add *abc*
Exclude *abc_def*

S3 cli includes not working

Alright I'm very confused by aws cli
I have an S3 bucket:
s3://my-bucket
directory/
file1
file2
backup-logs-1234
backup-logs-5678
I've verified that the files are in the s3 bucket, and I can see them with aws s3 ls s3://my-bucket
I'm trying to delete all the backup logs in the folder (8000 of them). I've tried every combination of includes/excludes I can think of
1) For some reason aws s3 rm "s3://my-bucket/" --include "*backup-logs*" --dryrun tries to delete s3://my-bucket/directory/
2) aws s3 rm "s3://my-bucket/" --exclude "*" --include "*backup-logs*" --dryrun doesn't see any files to delete
3) I've also tried different substrings of "backup" (eg. b, ba, back)
4) I've also tried adding recursive (even though I don't want it to be) and it finds all the files in directory/ that match the pattern, but none of the top level ones
I'm sure I'm doing something stupid. Thanks in advance for the help
aws s3 rm s3://my-bucket/ --recursive --exclude "*" --include "*backup-logs*" should work.
When you want to delete multiple objects within your bucket
--recursive (boolean) Command is performed on all files or objects
under the specified directory or prefix.
You can read on http://docs.aws.amazon.com/cli/latest/reference/s3/index.html#use-of-exclude-and-include-filters about include/exclude use

Glob pattern with amazon s3

I want to move files from one s3 bucket to another s3 bucket.I want to move only files whose name starts with "part".I can do it by using java.But is it possible to do it with amazon CLI. Can we use GlobPattern in CLI.
my object name are like:
part0000
part0001
Yes, this is possible through the aws CLI, using the --include and --exclude options.
As an example, you can use the aws s3 sync command to sync your part files:
aws s3 sync --exclude '*' --include 'part*' s3://my-amazing-bucket/ s3://my-other-bucket/
You can also use the cp command, with the --recursive flag:
aws s3 cp --recursive --exclude '*' --include 'part*' s3://my-amazing-bucket/ s3://my-other-bucket/
Explanation:
aws: The aws CLI command
s3: The aws service to interface with
sync: The command to the service to do
--exclude <value>: The UNIX-style wildcard to ignore, except by include statements
--include <value>: The UNIX-style wildcard to act upon.
As noted in the documentation, you can also specify --include and --exclude multiple times.

uploading all files of a certain extension type

I'm trying to upload all files of type .flv to an S3 bucket using the AWS CLI from a Windows server 2008 command line.
I do this:
aws s3 sync . s3://MyBucket --exclude '*.png'
And it begins uploading .png files instead.
I'm trying to follow the documentation and it gives an example that reads:
Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt
'''
aws s3 sync . s3://MyBucket/MyFolder --exclude '*.txt'
upload: MyFile2.rtf to s3://MyBucket/MyFolder/MyFile2.rtf
So what am I doing wrong?
Use:
aws s3 sync . s3://MyBucket/ --exclude "*" --include "*.flv"
It excludes all files, then includes .flv files. The order of parameters is important.
You can also use:
aws s3 cp . s3://MyBucket/ --recursive --exclude "*" --include "*.flv"
The difference is that sync will not re-copy a file that already exists in the destination.