Restrict file uploading to bucket root (pre-signed Form) - amazon-web-services

Is there a way to restrict the file upload via html forms (pre-signed POST) to the root folder?
The docs describes uploading into a specific folder with the starts-with condition.
But I can't see a way to restrict uploading to the bucket root (or any specific folder, without the ability to generate additional subfolders).
This bucket is only for uploading and any files that are accepted will move into another bucket. Because of that I don't want folders in there....

You will need an S3 Policy to deny actions where the resource contains a /. Add this to your S3 Bucket resource policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyObjectOthers",
"Effect": "Deny",
"Principal": "*",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::bucket/*/*"
]
}
]
}

Related

Restricting file types on amazon s3 bucket to specific folder with a policy

I am trying to create a bucket policy that allows the whole account access to upload objects, but limit a specific folder to a specific file ext, for example:
arn:aws:s3:::DOC-EXAMPLE-BUCKET/prefixname/*.jpg
However, after following the below example, it seems this is either only possible at a bucket level, or it restricts uploading specifically to the resource specified.
Example:
Restricting file types on amazon s3 bucket with a policy
My bucket policy looks like this:
{
"Version": "2012-10-17",
"Id": "Policy1464968545158",
"Statement": [
{
"Sid": "Stmt1464968483619",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:root"
},
"Action": "s3:PutObject",
"Resource": [
"arn:aws:s3:::DOC-EXAMPLE-BUCKET/folder/*.csv"
]
},
{
"Sid": "Stmt1464968483619",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"NotResource": [
"arn:aws:s3:::DOC-EXAMPLE-BUCKET/folder/*.csv"
]
}
]
}
Now with the above policy, it limits only uploads to that specific prefix.
I have tried using this in conjunction with a IAM policy, but the DENY statement pretty much over rides it.
I have also tried to leave out the deny statement and then use an IAM policy to grant access to the bucket and the resources, but my IAM user is still able to upload all extension types to all the different prefixes.
Has anyone been able to get this working at an account level, and not a user level?

AWS S3 IAM policy to limit to single sub folder

Scenario: I have a simple S3 bucket that multiple users will be uploading files to. Each user should be uploading to a specific folder and only that folder - no sub folders beyond that. Inside that folder, they can upload anything they want. I have an IAM policy that currently limits to that users folder, but allows them to specify sub folders, which I do not want.
Current IAM Policy JSON which limits to a top level folder:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObjectTagging"
],
"Resource": "arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/*"
}]
}
Proposed IAM Policy JSON which I expected to further limit PutObject only on the folder specified, but this doesn't seem to allow uploading of any object?:
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObjectTagging"
],
"Resource": "arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/*",
"Condition": {
"StringEquals": {
"s3:prefix": [
"",
"[MY_FOLDER]/"
],
"s3:delimiter": [
"/"
]
}
}
}]
}
Expected Results
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].csv
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].parquet
ALLOW arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FOLDER1]/[MY FOLDER2]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[MY_FOLDER1]/[MY_FILE].txt
DENY arn:aws:s3:::[MY_BUCKET]/[MY_FOLDER]/[...N Folders]/[MY_FILE].txt
There are no 'folders' in Amazon S3 as there are in a file system. What you see as folders, actually are just prefixes to the filename. The way, Amazon S3 works, you can restrict access to specific prefixes, but can't restrict the creation of additional prefixes underneath.
However, you can implement your requirement by utilizing Amazon S3 Events and a Lambda function. The process could look like this:
1. User stores file in Amazon S3
2. Amazon S3 fires an event notification, containing metadata, including object key (which represents the filename) including prefixes (which represent the folders).
3. Lambda function triggered by the S3 Event and processes the metadata by:
checking the filename for subsequent slashes (/) after the allowed prefix (which indicates a 'subfolder')
deleting the created file if it contains subsequent slashes

Allow listing of subfolders on AWS S3

I'm having an issue with hosting a static website on Amazon S3. When forwarding to a folder containing a index.html file it always returns a AccessDenied response.
So accessing domain.com/en/index.html works but domain.com/en gives AccessDenied.
Anyone an idea?
My bucket policy is
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::domain.com/*"
]
}
]
}
Your bucket policy is correct. What is happening is that domain.com/en doesn't forward your request to domain.com/en/index.html, so Amazon S3 thinks that you're requesting the object en inside the bucket's root folder.
To forward your request to index.html put a slash after your initial path, like domain.com/en/.
Check this AWS documentation below to more information:
https://docs.aws.amazon.com/AmazonS3/latest/dev/IndexDocumentSupport.html

Remove AWS S3 bucket having Read by Everyone permission through Bucket policy

Suppose I have an S3 bucket that has "Everyone Read" permission. Bucket is not public. Means anyone can access objects by typing its url in the browser. Now I want to remove this access from URL thing in browser. One option is to go to each images and remove "Read" from "Everyone" section. But since there are huge amount of images so this is not feasible.
So can I put such bucket policy which allows access only from one IAM user and not from browser thing? I tried adding such bucket policy that allow access to all resources for only specific user but still images are accessible from browsing through URL. Any thoughts?
Edit: Adding policy that I tried
{
"Id": "Policy1",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1",
"Action": "s3:*",
"Effect": "Allow",
"Resource": "arn:aws:s3:::test-bucket-public-issue",
"Principal": {
"AWS": [
"arn:aws:iam::AccounId:user/Username"
]
}
}
]
}
Ok #Himanshu Mohan I will explain you what i have done. I have created a S3 bucket and then i added the below bucket policy
{
"Version": "2012-10-17",
"Id": "Policy1534419239074",
"Statement": [
{
"Sid": "Stmt1534419237657",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::xxx-xxx-test/*"
}
]
}
While adding this policy the bucket will automatically public
Then i have uploaded an image as what you referred and i was able to access the same image via browser.
Now I changed the policy back to as what you said
Now i was not able to access the image, will show the access denied xml response. The only difference i see is i have added the /* after the bucket name "Resource": "arn:aws:s3:::xxx-xxx-test/*".

Need particular folder level access in S3

I am creating a user test in AWS IAM access . Also create a bucket name AWS-test,Under this bucket there is a folder called 'newfol' . I want to give permission to test user to particular newfol folder . test user only can upload file in newfol folder and also that user not able to see any other bucket or any other folder which is present under AWS-test .
I am written below json for that . But using that I able to enter AWS-test bucket and check all folder over there and upload file in all folder under AWs-test .
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::AWS-test"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::AWS-test/newfol/*"
}
]
}
The first part of your json will get a list of all objects in the AWS-test bucket. If you just want them to be able to upload to the newfol folder, then delete the first part of the json and it should work.