用aws凭证创建一个循环- Boto3



我正在尝试创建一个循环来切换~/上的每个帐户。Aws/credentials,我的Aws凭证有64个帐户,对于每个帐户,我想列出所有的bucket。

# !/usr/bin/env python
import os.path
import boto3
path = "~/.aws/credentials"
full_path = os.path.expanduser(path)
print(full_path)
with open(full_path, 'r') as f:
aws_account = f.read()
for account in aws_account:
s3 = boto3.client('s3')
response = s3.list_buckets()
buckets = [bucket['Name'] for bucket in response['Buckets']]
print("Bucket List: %s" % buckets)

AWS凭证示例:

[TEST1]
aws_access_key_id = TEST1
aws_secret_access_key = TEST1
[TEST2]
aws_access_key_id = TEST2
aws_secret_access_key = TEST2
[TEST3]
aws_access_key_id = TEST3
aws_secret_access_key = TEST3

我是这样做的。

#!/usr/bin/python3
import os.path
path = "~/.aws/credentials"
full_path = os.path.expanduser(path)
aws_account = []
with open(full_path, 'r') as f:
lines = f.readlines() 
f.close()
aws_account = [line[1:-2] for line in lines if line.startswith('[') ]
#print(aws_account)

相关内容

最新更新