AWS CLI 是什么云稚?
官網(wǎng)文檔:https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/cli-chap-welcome.html
AWS Command Line Interface (AWS CLI) 是一種開(kāi)源工具沈堡,讓您能夠在命令行 Shell 中使用命令與 AWS 服務(wù)進(jìn)行交互。
安裝 AWS CLI
- 在Python虛擬環(huán)境中安裝 AWS CLI
$ pip install awscli
Python虛擬環(huán)境相關(guān)鲸拥,參考: http://www.reibang.com/p/d66fce9a7bdc
查看當(dāng)前版本
$ aws --version
升級(jí)到最新版
$ aws install awscli --upgrade
卸載
$ pip uninstall awscli
配置 AWS CLI
- 添加默認(rèn)的配置文件
未使用過(guò) AWS CLI僧免,則必須先配置默認(rèn)的 CLI 配置文件
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-east-2
Default output format [None]: json
- 為新角色添加配置文件(如其他 IAM 角色)
- 在 .aws/config 文件中聲明新賬號(hào)所在區(qū)域。
[default]
region=ap-northeast-1
[profile ohagi3]
region=ap-northeast-1
- 在 .aws/credentials 文件中配置其他 IAM 角色的密鑰角撞。
[default]
aws_access_key_id=*******
aws_secret_access_key=*******
[ohagi3]
aws_access_key_id=*******
aws_secret_access_key=*******
- 指定參數(shù) --profile default 將命令附加到其他 IAM 角色
$ aws s3 ls --profile ohagi3
使用 AWS CLI
與 S3 結(jié)合使用
列出存儲(chǔ)桶
$ aws s3 ls
列出某個(gè)存儲(chǔ)桶中的內(nèi)容
$ aws s3 ls s3://my-bucket
上傳文件到s3存儲(chǔ)桶
$ aws s3 cp my-file s3://my-bucket/my-folder
與 DynamoDB 結(jié)合使用
官方文檔https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Tools.CLI.html
查看幫助
$ aws dynamodb help
顯示表列表
$ aws dynamodb list-tables
往表中插入一條數(shù)據(jù)
$ aws dynamodb put-item \
--table-name Music \
--item \
'{"Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"}}' \
--return-consumed-capacity TOTAL
- 查詢(xún)出Music表中所有數(shù)據(jù)
$ aws dynamodb scan --table-name Music
- 指定條件查詢(xún)
例如:從 Music 表中谒所,查詢(xún)出 Artist = "No One You Know" 并且 SongTitle = "Call Me Today" 的數(shù)據(jù)。
key-conditions.json
{
"Artist": {
"AttributeValueList": [
{
"S": "No One You Know"
}
],
"ComparisonOperator": "EQ"
},
"SongTitle": {
"AttributeValueList": [
{
"S": "Call Me Today"
}
],
"ComparisonOperator": "EQ"
}
}
$ aws dynamodb query --table-name Music --key-conditions file://key-conditions.json
- 批量寫(xiě)入
例如:將文件request-items.josn中所有Item批量寫(xiě)入 Service 表中劣领。
request-items.json
以下json數(shù)據(jù)中尖淘,
"Service" :是表名稱(chēng),可變村生。
"PutRequest": 表示 此Item為寫(xiě)入操作,固定格式趁桃。
"Item" :一條數(shù)據(jù)的key,固定格式油啤。
{
"Service": [
{
"PutRequest": {
"Item": {
"service_id":{"S": "id_1"},
"Artist": {"S": "No One You Know"},
"SongTitle": {"S": "Call Me Today"},
"AlbumTitle": {"S": "Somewhat Famous"}
}
}
},
{
"PutRequest": {
"Item": {
"service_id":{"S": "id_2"},
"Artist": {"S": "Acme Band"},
"SongTitle": {"S": "Happy Day"},
"AlbumTitle": {"S": "Songs About Life"}
}
}
},
{
"PutRequest": {
"Item": {
"service_id":{"S": "id_3"},
"Artist": {"S": "No One You Know"},
"SongTitle": {"S": "Scared of My Shadow"},
"AlbumTitle": {"S": "Blue Sky Blues"}
}
}
}
]
}
$ aws dynamodb batch-write-item --request-items file://request-items.json