当前位置: 首页 > 工具软件 > aws-sam-cli > 使用案例 >

AWS CLI 用法

芮立果
2023-12-01

一、安装

ubuntu:

pip3 install awscli --upgrade --user

mac os:

官方教程:Install, Update, and Uninstall the AWS CLI version 1 on macOS


二、配置

aws configure

AWS Access Key ID [None]: <your assess key>
AWS Secret Access Key [None]: <your secret assess key>
Default region name [None]: cn-northwest-1
Default output format [None]:

配置信息会记录在 ~/.aws/credentials 和 ~/.aws/config 中

cat ~/.aws/credentials

[default]
aws_access_key_id = xxxxxxxx
aws_secret_access_key = xxxxxxxx

cat ~/.aws/config

[default]
region = cn-northwest-1
output = json

多个命名配置文件

当有多个用户时(如新加alan账号),可在 ~/.aws/credentials 和 ~/.aws/config 中新加配置信息

cat ~/.aws/credentials

[default]
aws_access_key_id = xxxxxxxx
aws_secret_access_key = xxxxxxxx
[alan]
aws_access_key_id = YYYYYYYY
aws_secret_access_key = YYYYYYYY

cat ~/.aws/config

[default]
region = cn-northwest-1
output = json
[profile alan]
region = cn-northwest-1
output = json

在shell运行命令时,指定需要的profile

aws s3 cp XX XX --profile alan # 默认为--profile default
aws s3 cp XX XX --profile alan --endpoint https://xxx.xxx.com

shell读取aws配置信息

aws configure set aws_access_key_id $(aws configure get aws_access_key_id --profile aws) --profile default
aws configure set aws_secret_access_key $(aws configure get aws_secret_access_key --profile aws) --profile default

python读取aws配置信息

#!/usr/bin/env python3

import os

def get_aws_info(file_path, aws_info):
  for line in open(file_path):
    if '[' in line and ']' in line:
      default = line.split('[')[1].split(']')[0].split()[-1]
      aws_info["default"] = default
    else:
      items = [x.strip() for x in line.strip('\n').split('=')]
      if len(items) == 2:
        aws_info[items[0]] = items[1]

config_path = os.path.expanduser("~") + '/.aws/config'
credential_path = os.path.expanduser("~") + '/.aws/credentials'

aws_info = {}
get_aws_info(config_path, aws_info)
get_aws_info(credential_path, aws_info)
 
default = aws_info["default"]
region = aws_info["region"]
output = aws_info["output"]
aws_access_key_id = aws_info["aws_access_key_id"]
aws_secret_access_key = aws_info["aws_secret_access_key"]

c++读取aws配置信息

#include "aws/core/auth/AWSCredentialsProvider.h"
#include "aws/core/config/AWSProfileConfigLoader.h"
#include "aws/core/utils/logging/AWSLogging.h"
#include "aws/core/utils/logging/DefaultLogSystem.h"

Aws::Config::AWSConfigFileProfileConfigLoader loader(profile_path.c_str(), true);
bool state = loader.Load();
Aws::Map<Aws::String, Aws::Config::Profile> profiles = loader.GetProfiles();
Aws::Config::Profile profile = profiles.at("default");
Aws::Auth::AWSCredentials credentials = profile.GetCredentials();

三、使用

0、帮助文档

aws help
aws s3 help
aws s3 cp help

1、dynamodb用法

官方教程:dynamodb — AWS CLI 1.22.26 Command Reference

结合使用 Amazon DynamoDB 与 Amazon CLI - Amazon Command Line Interface

# list tables
aws dynamodb list-tables

# create table
aws dynamodb create-table \
--table-name alan_test \
--attribute-definitions AttributeName=id,AttributeType=N AttributeName=name,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH AttributeName=name,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--tags Key=Owner,Value=alan

# check table
aws dynamodb describe-table --table-name alan_test

# put item
aws dynamodb put-item \
--table-name alan_test \
--item '{ "id": {"N": "1"},"name":{"S":"alan"},"age":{"N":"20"}}' \
--return-consumed-capacity TOTAL

# update item
aws dynamodb update-item \
--table-name alan_test \
--expression-attribute-names '{"#class":"class"}' \
--key '{"id":{"N":"1"}, "name":{"S":"alan"}}' \
--update-expression "SET age = :a, #class = :c" \
--expression-attribute-values '{":a":{"N":"25"}, ":c":{"N":"4"}}'

# get item
aws dynamodb get-item \
--table-name alan_test \
--key '{"id":{"N":"1"}, "name":{"S":"alan"}}' \
--consistent-read \
--return-consumed-capacity TOTAL

# query items
aws dynamodb query \
--table-name alan_test \
--key-conditions '{"id":{"AttributeValueList":[{"N":"1"}],"ComparisonOperator":"EQ"}}'

# scan items
aws dynamodb scan --table-name alan_test

# delete item
aws dynamodb delete-item \
--table-name alan_test \
--key '{"id":{"N":"1"}, "name":{"S":"alan"}}'

# delete table
aws dynamodb delete-table --table-name alan_test

2、s3用法

官方教程:s3 — AWS CLI 1.22.26 Command Reference

通过 Amazon CLI 使用高级别 (s3) 命令 - Amazon Command Line Interface

# 列出所有桶
aws s3 ls
# 列出桶下面的所有文件夹(路径以"/"结尾)
aws s3 ls s3://<bucket_name>/
# 迭代查找指定目录下以test开头的所有文件
aws s3 ls s3://<bucket_name>/<directory_name>/ --recursive | grep "test*"
# 查看指定目录下的文件大小
aws s3 ls s3://<bucket_name>/<directory_name>/ --summarize --human-readable --recursive
# 文件重命名
aws s3 mv s3://<bucket_name>/<file_path>/<file_name_1> s3://<bucket_name>/<file_path>/<file_name_2>
# 拷贝/下载文件到当前目录
aws s3 cp s3://<bucket_name>/<file_path>/<file_name> .
# 上传文件
aws s3 cp /<local_path>/<file_name> s3://<bucket_name>/<directory_name>/
# 只显示 error 和 warning 信息
aws s3 cp xx xx --only-show-errors
# 上传文件夹
aws s3 sync /<local_path>/<directory_name> s3://<bucket_name>/<directory_name>
# 删除文件
aws s3 rm s3://<bucket_name>/<file_path>/<file_name>
# 删除整个文件夹
aws s3 rm s3://<bucket_name>/<directory_name>/ --recursive
# 删除文件夹中指定的文件
aws s3 rm s3://<bucket_name>/<directory_name>/ --recursive --exclude "**" --include "*.txt"
aws s3 rm s3://<bucket_name>/<directory_name>/ --recursive --exclude "*" --include "*.txt"
# 删除文件夹中的文件,保留指定的文件
aws s3 rm s3://<bucket_name>/<directory_name>/ --recursive --exclude "*.txt"
# 当运行命令出错时,加上--debug可获得关于该错误的更多详细信息。
aws s3 cp xx xx --debug
# --no-progress 不显示文件传输进度。此标志仅在未提供quiet和only-show-errors标志时应用。

3、sqs用法

官方教程:sqs — AWS CLI 1.22.26 Command Reference 

# list queue
aws sqs list-queues

# create queue
aws sqs create-queue \
--queue-name alan_test \
--attributes '{"DelaySeconds":"0","VisibilityTimeout":"18000","MessageRetentionPeriod":"1209600"}'

# get queue url
aws sqs get-queue-url --queue-name alan_test

# get queue attributes
aws sqs get-queue-attributes --queue-url https://sqs.cn-northwest-1.amazonaws.com.cn/111222333444/alan_test --attribute-names All
output:
{
    "Attributes": {
        "ApproximateNumberOfMessagesDelayed": "0",
        "DelaySeconds": "0",
        "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws-cn:sqs:cn-northwest-1:111222333444:alan_test-DLQ\",\"maxReceiveCount\":2}",
        "ApproximateNumberOfMessagesNotVisible": "0", # Messages in flight
        "MessageRetentionPeriod": "1209600",
        "ApproximateNumberOfMessages": "0", # Messages available
        "QueueArn": "arn:aws-cn:sqs:cn-northwest-1:111222333444:alan_test",
        "CreatedTimestamp": "1621060250",
        "MaximumMessageSize": "262144",
        "ReceiveMessageWaitTimeSeconds": "0",
        "VisibilityTimeout": "18000",
        "LastModifiedTimestamp": "1621240959"
    }
}

# send message
aws sqs send-message \
--queue-url https://cn-northwest-1.queue.amazonaws.com.cn/111222333444/alan_test \
--message-body "hello, world !"

# receive message
aws sqs receive-message \
--queue-url https://cn-northwest-1.queue.amazonaws.com.cn/111222333444/alan_test \
--attribute-names All \
--message-attribute-names All \
--max-number-of-messages 1

# delete message
aws sqs delete-message \
--queue-url https://cn-northwest-1.queue.amazonaws.com.cn/111222333444/alan_test \
--receipt-handle AQEBRXTo...q2doVA==

# purge queue
aws sqs purge-queue --queue-url https://sqs.cn-northwest-1.amazonaws.com.cn/111222333444/alan_test

# delete queue
aws sqs delete-queue --queue-url https://cn-northwest-1.queue.amazonaws.com.cn/111222333444/alan_test

参考:

https://docs.aws.amazon.com/zh_cn/cli/index.html

 类似资料: