rreddy
rreddy

Reputation: 55

copy file to AWS S3 bucket

Is there a way to copy a file from my local to S3 bucket by passing I am access keys through command line .Like

cp $file_name AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY $S3_BUCKETNAME

Tried above command but never worked

Upvotes: 3

Views: 3780

Answers (1)

Matei David
Matei David

Reputation: 2362

First, install awscli (documentation):

pip install [--user] awscli

Then:

export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
aws s3 cp <file> <S3Uri>

Credentials can also be specified using profiles in the aws configuration file (documentation):

cat <<EOF >~/.aws/config
[profile test1]
aws_access_key_id=foo1
aws_secret_access_key=bar1
[profile test2]
aws_access_key_id=foo2
aws_secret_access_key=bar2
EOF
aws --profile test1 s3 cp <uri1> <file>
aws --profile test2 s3 cp <file> <uri2>

Note that:

Credentials from environment variables have precedence over credentials from the shared credentials and AWS CLI config file.

Thus, to use multiple credential sets, make sure none of them are passed as environment variables, because they would override those from the configuration file.

Upvotes: 5

Related Questions