aws CLI使用
官方網站:https://docs.aws.amazon.com/cli/latest/reference/s3/
AWS CLI 引進了一組新的簡單
常用命令
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
新的
$ 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.
如果分段上傳由於超時而失敗,或者您在 Amazon CLI 中手動取消該操作,則 Amazon CLI 會停止上傳並清除已創建的任何文件。此過程可能耗時數分鍾。
如果使用 kill 命令或者由於系統故障而取消了分段上傳或清理過程,則創建的文件將保留在 Amazon S3 存儲桶中。要清除分段上傳,請使用
有關更多信息,請參閱 Amazon Simple Storage Service 用戶指南中的