Search code examples
jsondictionaryamazon-s3boto3aws-organizations

boto3 how to upload dict / json output to s3 bucket?


I have troubles to upload my output which is dict from organizations describe_policy to s3. here is my code

paginator = org.get_paginator('list_policies')
page_iterator = paginator.paginate(Filter='SERVICE_CONTROL_POLICY', PaginationConfig={'MaxItems': 10000})
for page in page_iterator:
    for id in page['Policies']:
        policyid = id['Id']
        describe = org.describe_policy(PolicyId=policyid)
        s3.put_object(Body=describe,Bucket='cf-asr-demo',Key=day_month_year+policyid+'/describe-scp.csv')

The error says that my output is dict class

botocore.exceptions.ParamValidationError: Parameter validation failed:
type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object

How do I upload this output that is basically just a description of a policy to the bucket?


Solution

  • So I just fixed it myself, I dumped into a json

    policy = json.dumps(describe)
    

    and than it worked