AWS CLI 基本使用

2019-06-06  本文已影响0人  and2long

AWS CLI 是什么?

官网文档:https://docs.aws.amazon.com/zh_cn/cli/latest/userguide/cli-chap-welcome.html

AWS Command Line Interface (AWS CLI) 是一种开源工具,让您能够在命令行 Shell 中使用命令与 AWS 服务进行交互。

安装 AWS CLI

Python虚拟环境相关,参考: https://www.jianshu.com/p/d66fce9a7bdc

配置 AWS 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
创建需要的密钥
  1. 在 .aws/config 文件中声明新账号所在区域。
[default]
region=ap-northeast-1

[profile ohagi3]
region=ap-northeast-1
  1. 在 .aws/credentials 文件中配置其他 IAM 角色的密钥。
[default]
aws_access_key_id=*******
aws_secret_access_key=*******

[ohagi3]
aws_access_key_id=*******
aws_secret_access_key=*******

使用 AWS CLI

与 S3 结合使用

与 DynamoDB 结合使用

官方文档https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/Tools.CLI.html

$ 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  

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

request-items.json

以下json数据中,
"Service" :是表名称,可变。
"PutRequest": 表示 此Item为写入操作,固定格式。
"Item" :一条数据的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

上一篇 下一篇

猜你喜欢

热点阅读