AWS SDK 如何在不指定凭据的情况下知道凭据?
How does AWS SDK know the credentials without specifying it?
我很好奇 AWS SDK 如何在不显式提供凭据的情况下访问本地服务(例如 S3)。例如,此 python 代码仅提供存储桶名称和密钥名称,但仍然可以从我本地的 s3 访问文件:
def s3():
bucket = "my-bucket"
file_name = "folder1/sample.json"
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=file_name)
file_content = obj["Body"].read().decode('utf-8')
AWS SDK 从哪里获得凭证?它是否使用在 CLI 中使用命令 aws configure
配置的角色?如果您提供显式访问密钥和秘密密钥,优先级是多少?
所有亚马逊 SDK 都遵循类似的模式。对于 boto3,它们被记录在案 here 但为了完整起见,它们是:
- Passing credentials as parameters in the boto.client() method
- Passing credentials as parameters when creating a Session object
- Environment variables
- Shared credential file (~/.aws/credentials)
- AWS config file (~/.aws/config)
- Assume Role provider
- Boto2 config file (/etc/boto.cfg and ~/.boto)
- Instance metadata service on an Amazon EC2 instance that has an IAM role configured.
这取决于您的环境配置方式,但听起来您有一个 ~/.aws/credentials
文件。
我很好奇 AWS SDK 如何在不显式提供凭据的情况下访问本地服务(例如 S3)。例如,此 python 代码仅提供存储桶名称和密钥名称,但仍然可以从我本地的 s3 访问文件:
def s3():
bucket = "my-bucket"
file_name = "folder1/sample.json"
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket, Key=file_name)
file_content = obj["Body"].read().decode('utf-8')
AWS SDK 从哪里获得凭证?它是否使用在 CLI 中使用命令 aws configure
配置的角色?如果您提供显式访问密钥和秘密密钥,优先级是多少?
所有亚马逊 SDK 都遵循类似的模式。对于 boto3,它们被记录在案 here 但为了完整起见,它们是:
- Passing credentials as parameters in the boto.client() method
- Passing credentials as parameters when creating a Session object
- Environment variables
- Shared credential file (~/.aws/credentials)
- AWS config file (~/.aws/config)
- Assume Role provider
- Boto2 config file (/etc/boto.cfg and ~/.boto)
- Instance metadata service on an Amazon EC2 instance that has an IAM role configured.
这取决于您的环境配置方式,但听起来您有一个 ~/.aws/credentials
文件。