返回顶部

aws CLI使用

aws CLI使用

官方网站:https://docs.aws.amazon.com/cli/latest/reference/s3/

AWS 命令行界面 (CLI) 是用于管理 AWS 服务的统一工具。只通过一个工具进行下载和配置,您可以使用命令行控制多个 AWS 服务并利用脚本来自动执行这些服务。

AWS CLI 引进了一组新的简单文件命令,用于改善与 Amazon S3 的双向文件传输效率

常用命令

cp、ls、mb、mv、presign、rb、rm、sync、website

查看支持的服务,获取帮助
$ aws help

服务的相关操作
$ aws autoscaling help

以及某个服务操作的参数
$ aws autoscaling create-auto-scaling-group help

查看当前版本
$ aws --version

升级到最新版
$ aws install awscli --upgrade

卸载
$ pip uninstall awscli

 

添加默认的配置文件
$ 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 文件中声明新账号所在区域
[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=*******

指定参数 --profile default 将命令附加到其他 IAM 角色
$ aws s3 ls --profile ohagi3

列出存储桶
$ aws s3 ls

列出某个存储桶中的内容
$ aws s3 ls s3://my-bucket

上传文件到s3存储桶
$ aws s3 cp my-file s3://my-bucket/my-folder

awscli s3

新的文件命令可以轻松管理您的 Amazon S3 数据元。您可以使用熟悉的语法在基于目录的列表中查看 S3 存储段的内容。

$ aws s3 cp myfolder s3://mybucket/myfolder --recursive
上传:myfolder/file1.txt 到 s3://mybucket/myfolder/file1.txt
上传:myfolder/subfolder/file1.txt 到 s3://mybucket/myfolder/subfolder/file1.txt
​
$ aws s3 sync myfolder s3://mybucket/myfolder --exclude *.tmp
上传:myfolder/newfile.txt 到 s3://mybucket/myfolder/newfile.txt [同步本地文件时间戳最新]

官方帮助文档:

https://docs.aws.amazon.com/cli/latest/reference/s3/{cp,ls,mb,mv,presign,rb,rm,syncwebsite}.html

示例

#只上传以.jpg结尾的文件
aws s3 cp /tmp/foo/ s3://bucket/ --recursive --exclude "*" --include "*.jpg"

#同时上传包含.jpg文件和.txt文件
aws s3 cp /tmp/foo/ s3://bucket/ --recursive \
    --exclude "*" --include "*.jpg" --include "*.txt"

AWS常用选项

官方选项帮助文档链接:https://docs.aws.amazon.com/cli/latest/reference/#available-services

--debug (boolean)
Turn on debug logging.

--endpoint-url (string)
Override command's default URL with the given URL.

--no-verify-ssl (boolean)
By default, the AWS CLI uses SSL when communicating with AWS services. For each SSL connection, the AWS CLI will verify SSL certificates. This option overrides the default behavior of verifying SSL certificates.

--no-paginate (boolean)
Disable automatic pagination.

--output (string)
The formatting style for command output.

json
text
table
--query (string)
A JMESPath query to use in filtering the response data.

--profile (string)
Use a specific profile from your credential file.

--region (string)
The region to use. Overrides config/env settings.

--version (string)
Display the version of this tool.

--color (string)
Turn on/off color output.

on
off
auto
--no-sign-request (boolean)
Do not sign requests. Credentials will not be loaded if this argument is provided.

--ca-bundle (string)
The CA certificate bundle to use when verifying SSL certificates. Overrides config/env settings.

--cli-read-timeout (int)
The maximum socket read time in seconds. If the value is set to 0, the socket read will be blocking and not timeout. The default value is 60 seconds.

--cli-connect-timeout (int)
The maximum socket connect time in seconds. If the value is set to 0, the socket connect will be blocking and not timeout. The default value is 60 seconds.

注意:

当您使用 aws s3 命令将大型对象上传到 Amazon S3 存储桶时,Amazon CLI 会自动执行分段上传。使用这些 aws s3 命令时,您无法恢复失败的上传操作。

如果分段上传由于超时而失败,或者您在 Amazon CLI 中手动取消该操作,则 Amazon CLI 会停止上传并清除已创建的任何文件。此过程可能耗时数分钟。

如果使用 kill 命令或者由于系统故障而取消了分段上传或清理过程,则创建的文件将保留在 Amazon S3 存储桶中。要清除分段上传,请使用 s3api abort-multipart-upload 命令。

有关更多信息,请参阅 Amazon Simple Storage Service 用户指南中的分段上传概述

 

posted @ 2021-12-01 09:55  九尾cat  阅读(2278)  评论(0编辑  收藏  举报