Cloudformation - Check if S3 folder exists and apply policy to the specific folder
Hello: I am writing a cloudformation template to create an S3 bucket. This S3 bucket can have multiple folders created programmatically. So I want to apply a bucket policy that checks if a specific folder exists and allow only specific file types. For ex, if there is an S3 bucket called 'my-test-s3' with nested folders /folder1/folder2/pdf, I want to check if the folder 'pdf' exists. If it does, only allow for file types of *.pdf in this folder. How do I write the bucket policy for this using cloudformation template? I tried this below. However, this prevents creation of any folders inside my 'S3Bucket'. I am looking for some guidance on this.
Sid: Stmt1631632005699 Effect: Deny Principal: AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root/' Action: - 's3:PutObject' NotResource: - !Sub arn:aws:s3:::${S3Bucket}///pdf/*.pdf
Something like this:
{
"Id": "Policy1649706507409",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1649706435908",
"Action": [
"s3:PutObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::mybucket/*.pdf",
"Condition": {
"StringLike": {
"s3:prefix": "pdf/"
}
},
"Principal": "*"
},
{
"Sid": "Stmt1649706506153",
"Action": [
"s3:PutObject"
],
"Effect": "Deny",
"NotResource": "arn:aws:s3:::mybucket/*.pdf",
"Condition": {
"StringLike": {
"s3:prefix": "pdf/"
}
},
"Principal": "*"
}
]
}
Relevant questions
Cloudtrail S3 bucket objects
asked a month agoS3 Notification event name through CloudFormation
asked 5 months agobulk move files from root of an S3 bucket to a folder (but not include other folders)
Accepted Answerasked 6 months agoCloudformation - Check if S3 folder exists and apply policy to the specific folder
Accepted Answerasked 3 months agoS3 Bucket List reveals top-level object names
Accepted Answerasked 6 years agoNot Able To Create a New Bucket - Policy Permissions Appear Correct
asked 8 months agoS3 bucket permissions to run CloudFormation from different accounts and create Lambda Funtions.
asked 6 months agoFile Gateway (SMB) and Folder level KMS (CMK) in bucket
asked a year agoCan AWS CloudFormation template access s3 from any region other than its current region?
asked 13 days agoConcurrent writes to the same folder on an S3 bucket using the JavaScript SDK v2
asked 23 days ago