403 when trying to view s3 files / images - amazon-web-services

I have the s3 url saved to my mongoose object, then on the client side, i'm attempting to use this s3 url as an src.
I keep getting a 403.
I've looked at a few similar questions, which state I need to configure my permissions / policy.
I've done that:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "UploadFile",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::acct#:root"
},
"Action": [
"s3:PutObject"
],
"Resource": "arn:aws:s3:::whiskey-upload/*"
},
{
"Sid": "crossdomainAccess",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::whiskey-upload/crossdomain.xml"
},
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::whiskey-upload/*"
}
]
}
Any clue on what else I may be doing wrong?

If you have it as http://www. As a prefix in src it wont work. I had encountered such problem before. You can test this directly too. Suppose you have an image src to the s3 bucket. Try to view the image on web browser with www. And without www. Prefix. You might understand better.
But if its directly s3.url then it should work. Please show me the src url that you have to debug the issue.

Related

Amazon S3 Bucket Policy Block User Agent

I need to block a specific user agent from amazon s3. I'm fairly new to S3 and typically do this in the .htaccess file which I understand isn't possible with amazon.
I saw this Deny access to user agent to access a bucket in AWS S3. The solution seems to look something like this:
{ "Version": "2012-10-17",
"Statement": [{
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*",
"Condition":
{ "StringLike": { "aws:UserAgent": "*NSPlayer*" } }
}]
}
But, I would still like to allow access to the site to all other users except that user agent. My policy currently looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::www.examplesite.com/*"
}
]
}
I get invalid json when I try to implement it. So, I am just curious what is the correct way to format this? I would like to have public access but be able to block a few different user agents. Also, would the user agents be comma separated if I want to block multiple?
Thanks for the help it is much appreciated.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::www.examplesite.com/*"
},
{
"Sid": "DenyUserAgents",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::www.examplesite.com/*",
"Condition": {
"StringLike": {
"aws:UserAgent": ["*Firefox*", ... more UserAgents ...]
}
}
}
]
}

How to enable AmazonS3 static website hosting?

My problems are that S3 Properties Tab shows
When I click use this and then save, button is not active any more.It changes color and I can not do anything about it.
This is the content of my S3
My S3 policy
{
"Version": "2012-10-17",
"Id": "Policy1575283327440",
"Statement": [
{
"Sid": "Stmt1575283033809",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::gitlab-jekyll-*********/*"
},
{
"Sid": "Stmt1575283267330",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::5771********:user/terraformadminuser"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::gitlab-jekyll-**********/*"
}
]
}
How to fix this? I can not show my bucket id because it has public access.
Type index.html to both input boxs, then you can save

Creating a S3 policy to allow full access to one S3 subfolder

I am trying to create a AWS S3 policy to allow full access to a specific S3 subfolder but nothing else. In the example below there is a dev named Bob. I created a directory dedicated entirely to Bob and want to give him full read/write access to this S3 folder only (the bob folder) by logging in to the console.
This is what I tried, although when attempting to access the bob directory I am getting an access denied error. I appreciate any suggestions on how to accomplish this.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::/mydir/devs/bob/*"
]
}
]
}
This is what I have for exactly the same need. Some of the combinations seem counterintuitive (why do I need both ListAllBuckets and ListBucket - but it seems to not work without it):
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListAllMyBuckets"
],
"Resource": [
"arn:aws:s3:::*"
]
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::mydir"
]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::mydir/devs/bob/*"
]
}
]
}
Also, I think ListBucket requires a bucket (that is, mydir in your example). I don't think it works with the key (or folder), like mydir/devs/bob
Remember S3 objects are the full key name, so you have given permissions to:
/mydir/devs/bob/*
Which represents all keys prefixed with /mydir/devs/bob/, but wouldn't include the key /mydir/devs/bob itself, which is your "folder". It's also missing a bucket name (bucket-name/mydir/devs/bob/*) This means the user can't use a list action on the "folder" or it's parent folders. So, Bob can't navigate to his folder.
There is a great walkthrough on creating user specific subfolders available on the AWS Blog, here. We can break that info down to fit your use case, the statement ids would look like this:
Allow Bob the needed permissions to see the bucket list in the console:
{
"Sid": "AllowUserToSeeBucketListInTheConsole",
"Action": ["s3:ListAllMyBuckets", "s3:GetBucketLocation"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::*"]
}
Allow Bob to navigate to his folder by allowing listing on each parent directory
{
"Sid": "AllowRootAndHomeListingOfCompanyBucket",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::bucket-name"],
"Condition":{"StringEquals":{"s3:prefix":["","mydir/","mydir/devs/","mydir/devs/bob"],"s3:delimiter":["/"]}}
}
Let Bob list any files and folders inside his folder
{
"Sid": "AllowListingOfUserFolder",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::bucket-name"],
"Condition":{"StringLike":{"s3:prefix":["mydir/devs/bob/*"]}}
}
Finally, let Bob take any actions on anything inside his folder
{
"Sid": "AllowAllS3ActionsInUserFolder",
"Action":["s3:*"],
"Effect":"Allow",
"Resource": ["arn:aws:s3:::bucket-name/mydir/devs/bob/*"]
}
This combines to look like this:
{
"Version":"2012-10-17",
"Statement": [
{
"Sid": "AllowUserToSeeBucketListInTheConsole",
"Action": ["s3:ListAllMyBuckets", "s3:GetBucketLocation"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::*"]
},
{
"Sid": "AllowRootAndHomeListingOfCompanyBucket",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::bucket-name"],
"Condition":{"StringEquals":{"s3:prefix":["","mydir/","mydir/devs/","mydir/devs/bob"],"s3:delimiter":["/"]}}
},
{
"Sid": "AllowListingOfUserFolder",
"Action": ["s3:ListBucket"],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::bucket-name"],
"Condition":{"StringLike":{"s3:prefix":["mydir/devs/bob/*"]}}
},
{
"Sid": "AllowAllS3ActionsInUserFolder",
"Action":["s3:*"],
"Effect":"Allow",
"Resource": ["arn:aws:s3:::bucket-name/mydir/devs/bob/*"]
}
]
}
The provided documentation also gives a great example on doing this by using the ${aws:username} variable inside the policy so this can be applied to groups.

Resize Images on the Fly with AWS Lambda and Amazon API Gateway

I followed the tutorial on this page HERE
but when I try to get a resized picture I get an "Access Denied"
Good: https://xxxx.amazonaws.com/mybucket/test.jpg
Error: https://xxxx.amazonaws.com/mybucket/300x300/test.jpg (access denied)
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
</Error>
Below my settings:
Bucket policy editor
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::mybucket/*"
}
]
}
When I created the trigger, I selected Security: OPEN. I'm just confused about the YOUR_API_HOSTNAME_HERE. In the example, the api hostname is h3ll0w0rld?
GetObject action is not enough. You should give lambda permission to list the content as well . Also notice Resources section that I put.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Principal": { "Service": "lambda.amazonaws.com" },
"Resource": [
"arn:aws:s3:::mybucket/*",
"arn:aws:s3:::mybucket"
]
}
]
}
#AbdennourTOUMI you're right. The "bucket policy" must be:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucketNAME/*"
}
]
}
Are you sure that your bucket contains 300x300 folder containing the file. because, as #Michael-sqlbot said, it can indicate that the file does not exist.
Yes, In the example, the api hostname is h3ll0w0rld.execute-api.us-west-2.amazonaws.com.
To get the resized picture, you need use your static website hosting endpoint - http://bucket_name.s3-website-us-west-2.amazonaws.com/300x300/test.jpg
then in your bucket will be created a folder 300x300, which will contain 'test.jpg'

S3 policy - public write but only authenticated read

What I am trying to do is to let (anonymous) users share files to a specified bucket. However, they should not be possible to READ the files, which are already there (and for all I care not even the ones they submitted themselves). The only account which should be able to list/get objects from the bucket should be the bucket owner.
Here is what I got so far:
{
"Version": "2012-10-17",
"Id": "PutOnlyPolicy",
"Statement": [
{
"Sid": "Allow_PublicPut",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": "arn:aws:s3:::myputbucket/*"
},
{
"Sid": "Deny_Read",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::myputbucket/*"
},
{
"Sid": "Allow_BucketOwnerRead",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::myAWSAccountID:root"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::myputbucket/*"
}
]
}
The above Policy enables me to write files to the bucket (f.e. via the android app S3anywhere), but I can't GET the objects, not even with my authenticated account.
Do you have any hints on how I could accomplish this? Thanks!
Anonymous users are not able to read a bucket content by default. So you should have only these lines in your policy:
{
"Version": "2012-10-17",
"Id": "PutOnlyPolicy",
"Statement": [
{
"Sid": "Allow_PublicPut",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:PutObject",
"s3:PutObjectAcl"
],
"Resource": "arn:aws:s3:::myputbucket/*"
}
]
}
The deny statement in your policy takes precedence over everything else. The default is to deny everything that isn't specifically allowed, so you should be able to just remove the deny statement and all will work the way you want.
Policy looks good, I guess that problem into Principal, you can look how it use into documentation http://docs.aws.amazon.com/AmazonS3/latest/dev/s3-bucket-user-policy-specifying-principal-intro.html. Probably you should use AccountNumber-WithoutHyphens