AWS CLI使用s3

简介:

1.安装CLI

文档:http://docs.aws.amazon.com/cli/latest/userguide/installing.html

1
2
3
$ 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就可以了。

aws configure

 

3.脚本配置

由于给我的key是1h的,因此需要每次使用的时候更新它。

脚本中使用环境变量是最好的。参考:http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environment

然后,我明明在脚本中export了key,却总是不通过。纠结了好久之后,百度了n个帖子之后,还是Linux环境变量的问题。

  • 子shell复制父shell的环境变量
  • export的环境变量是当前有效并且针对当前用户的
  • 脚本中aws默认寻找总的环境变量,这就导致自己设置的环境变量没有读取
  • 所以需要在脚本的开头声明环境变量文件:
    1
    2
    /etc/profile
    . ~/.bash_profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
 
# update aws configure
# configure environment variables for the default user
#  ~/.aws/credentials
# [default]
#   aws_access_key_id = AKIAJB2HUQ2N2SVSWDDA
#   aws_secret_access_key = DlzsCdXrCfeQ+dsAd69U1liDQxzRK6cMpY+3Cn64
#  ~/.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

 本文转自Ryan.Miao博客园博客,原文链接:http://www.cnblogs.com/woshimrf/p/5503406.html,如需转载请自行联系原作者 

相关文章
|
4月前
|
Kubernetes 监控 云计算
Docker与云计算平台集成:AWS、Azure、GCP完全指南
Docker和云计算平台的结合,如AWS(Amazon Web Services)、Azure(Microsoft Azure)和GCP(Google Cloud Platform),为现代应用的构建和部署提供了巨大的便利性。本文将深入研究如何与这些主要云计算平台集成Docker,提供更多示例代码和详细指南,帮助大家更全面地利用这些强大的工具。
|
9月前
|
存储 Kubernetes NoSQL
Kubernetes在AliCloud上部署并优化MongoDB
Kubernetes, 阿里云, MongoDB, 优化
194 0
EMQ
|
存储 JSON 安全
使用 Terraform 在 AWS 上快速部署 MQTT 集群
本文将以 MQTT 消息服务器 EMQX 为例,采用 AWS 作为公有云平台,介绍如何使用 Terraform 快速部署一个高可用的 MQTT 集群。
EMQ
205 0
使用 Terraform 在 AWS 上快速部署 MQTT 集群
|
Kubernetes 监控 NoSQL
在kubernetes集群中部署open-falcon
公司最近监控系统从zabbix切换到open-falcon,需要将open-falcon部署到私有的kubernetes集群上。open-falcon团队最近没有更新维护,提交的PR没有反应,所以将部署方法记录到这里。
318 0
在kubernetes集群中部署open-falcon
|
JavaScript Serverless Shell
Serverless CLI 命令
Serverless CLI 命令
267 0
|
Kubernetes Ubuntu 应用服务中间件
使用Gardener在Google Cloud Platform上创建Kubernetes集群
使用Gardener在Google Cloud Platform上创建Kubernetes集群
211 0
|
SQL Kubernetes 网络协议
在Google Cloud platform上的Kubernetes集群部署HANA Express
在Google Cloud platform上的Kubernetes集群部署HANA Express
123 0
在Google Cloud platform上的Kubernetes集群部署HANA Express
|
Web App开发 Java 数据安全/隐私保护