0
我想從我擁有的所有s3存儲桶中提取s3存儲桶策略,並將它們存儲在各自的.json文件中。這應該在json中格式化。但是,在我嘗試的解決方案中,它將策略存儲在帶有Response元數據的文件中,並且它不是jSOn格式。如何將S3存儲桶策略寫入文件?
"ResponseMetadata": {"HTTPStatusCode": 200, ...... "content-type": "application/json"}}}
這可以在python/boto3中完成嗎?這是我的代碼。
try:
s3client = get_s3Client('us-east-1')
response = s3client.list_buckets()
if response and response['Buckets']:
for bucket in response['Buckets']:
bucketName = bucket['Name']
print (bucketName)
policyname = policy_output + '\\' + bucketName + '.json'
print (policyname)
response = s3client.get_bucket_policy(
Bucket=bucketName
)
print (response)
with open(policyname, 'w') as f:
json.dump(response, f)
#f.write(response)
該解決方案的偉大工程,但它輸出的JSON的文件中一行。這可以格式化嗎? – Geddon
修改了包含JSON的漂亮打印。 – jarmod