AWS CLI 的安装和使用

一、安装

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

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"]

三、使用

查看用法

aws help
aws s3 help
aws s3 cp help

常用命令 

# 列出所有桶
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 "*.txt"
# 当运行命令出错时,加上--debug可获得关于该错误的更多详细信息。
aws s3 cp xx xx --debug

参考:

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

猜你喜欢

转载自blog.csdn.net/A_L_A_N/article/details/105178698