Skip to main content
Nordlys logo, a drawing of two gray mountains with green northern lights in the background 陈迪の自留地

Back to all posts

AWS-技术指北

Published on by Chen Di · 2 min read

Table of Contents

Show more

S3 最佳实践

1. ~/.aws/credentials 配置文件中添加密钥

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

2. 使用环境变量设置凭证

linux

export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY

windows

set AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY
set AWS_SECRET_ACCESS_KEY=YOUR_SECRET_KEY

3. 基于 IAM

如果代码在 AWS EC2 实例上运行,可以使用 IAM 角色分配访问权限。在这种情况下,boto3 会自动获取 IAM 角色分配的临时凭证,无需手动配置密钥。

awsLinux 默认安装 aws-cli

aws configure

AWS Access Key ID [None]: AKIATXBI6ZJCYEQxxxxxx
AWS Secret Access Key [None]: bh4Z5Xq9cM/U/7XZ5/Dkwqidxxxxxxx
Default region name [None]: me-central-1
Default output format [None]: json
aws sts get-caller-identity
{
    "UserId": "AIDATXBI6ZJCSxxxxx",
    "Account": "25563xxxx8053",
    "Arn": "arn:aws:iam::2xxx458053:user/prd-uz-s3user"
}

AWS CLI 安装

可以通过 aws-cli 在指定的机器上配置 IAM 凭证

windows 直接官网下载对应平台的安装包

AWS CLI MSI适用于 Windows 的安装程序(64 位):https://s3.amazonaws.com/aws-cli/AWSCLI64PY3.msi

AWS CLI 适用于 Windows 的组合安装文件: https://s3.amazonaws.com/aws-cli/AWSCLISetup.exe

linux 可以通过 pip 安装

pip3 install awscli --upgrade --user