AWS CLI使用s3

aws CLI是什麼東西,暫且先不去了解,目前的需求是s3.html

我在Jenkins上建立一個bucket,而後申請access_key,而後就能夠使用s3來存儲數據了。也就是說,s3就是一個網盤。mongodb

1.安裝CLI

文檔:http://docs.aws.amazon.com/cli/latest/userguide/installing.htmlshell

$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
$ unzip awscli-bundle.zip
$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

  

2.配置

配置很簡單,提供access_key 和secret_access_key以及region就能夠了。json

aws configurebash

 

3.腳本配置

因爲給個人key是1h的,所以須要每次使用的時候更新它。app

腳本中使用環境變量是最好的。參考:http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environmentcurl

而後,我明明在腳本中export了key,卻老是不經過。糾結了很久以後,百度了n個帖子以後,仍是Linux環境變量的問題。ide

  • 子shell複製父shell的環境變量
  • export的環境變量是當前有效而且針對當前用戶的
  • 腳本中aws默認尋找總的環境變量,這就致使本身設置的環境變量沒有讀取
  • 因此須要在腳本的開頭聲明環境變量文件:
    . /etc/profile
    . ~/.bash_profile
#!/bin/bash

# update aws configure
# configure environment variables for the default user
#  ~/.aws/credentials
# [default]
#   aws_access_key_id = AKIAJB2HUQ2N2SVSWDDA
#   aws_secret_access_key = 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
#  ~/.aws/config
# [default]
# output = json
# region = us-west-2

. /etc/profile
. ~/.bash_profile
#get client_token
 client_token_json=`curl -XPOST https://store.test.expedia.com/v1/auth/app-id/login -d '{"app_id":"d8xxxxxxxx789871","user_id":"710xxxxxxxxxb65-b8xxxxxaf1b01"}' --silent  -k`
 client_token=$(echo $client_token_json | jq .auth.client_token | cut -d "\"" -f 2)
 aws_auth=$(curl -H "X-Vault-Token:$client_token" -X GET   https://store.test.expedia.com/v1/test/creds/epc-content-mongodb --silent  -k)

# set environment, only effec in this shell
 access=$(echo $aws_auth | jq .data.access_key | cut -d "\"" -f 2)
 secret=$(echo $aws_auth | jq .data.secret_key | cut -d "\"" -f 2)

 export AWS_ACCESS_KEY_ID=$access
 export AWS_SECRET_ACCESS_KEY=$secret
 env | grep AWS


echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id="$access>>~/.aws/credentials
echo "aws_secret_access_key="$secret>>~/.aws/credentials

echo "[default]"> ~/.aws/config
echo "output = json">>~/.aws/config
echo "region = us-west-2">>~/.aws/config
 echo "aws key has updated!"

# wait for effective
#sleep 10s
#test aws s3
#aws s3 ls s3://epc-content-mongodb-export --output json --region us-west-2
相關文章
相關標籤/搜索