def make_policy(bucket_name, prefix, allow_get_location=False):
 bucket_arn = ('arn:aws:s3:::' + bucket_name)
 prefix_arn = 'arn:aws:s3:::{0}/{1}/*'.format(bucket_name, prefix)
 structure = {'Version': '2012-10-17', 'Statement': [{'Action': ['s3:ListBucket'], 'Effect': 'Allow', 'Resource': [bucket_arn], 'Condition': {'StringLike': {'s3:prefix': [(prefix + '/*')]}}}, {'Effect': 'Allow', 'Action': ['s3:PutObject', 's3:GetObject'], 'Resource': [prefix_arn]}]}
 if allow_get_location:
  structure['Statement'].append({'Action': ['s3:GetBucketLocation'], 'Effect': 'Allow', 'Resource': [bucket_arn]})
 return json.dumps(structure, indent=2)

def make_policy ( bucket_name , prefix , allow_get_location = False ) : bucket_arn = ( 'arn:aws:s3:::' + bucket_name ) prefix_arn = 'arn:aws:s3:::{0}/{1}/*' . format ( bucket_name , prefix ) structure = { 'Version' : '2012-10-17' , 'Statement' : [ { 'Action' : [ 's3:ListBucket' ] , 'Effect' : 'Allow' , 'Resource' : [ bucket_arn ] , 'Condition' : { 'StringLike' : { 's3:prefix' : [ ( prefix + '/*' ) ] } } } , { 'Effect' : 'Allow' , 'Action' : [ 's3:PutObject' , 's3:GetObject' ] , 'Resource' : [ prefix_arn ] } ] } if allow_get_location : structure [ 'Statement' ] . append ( { 'Action' : [ 's3:GetBucketLocation' ] , 'Effect' : 'Allow' , 'Resource' : [ bucket_arn ] } ) return json . dumps ( structure , indent = 2 )
produces a s3 iam text for selective access of data .