How to fix "Access denied" on opening image url - amazon-web-services

I finally managed to upload some images to my s3 bucket but I can't open them. If I navigate to them in my bucket i get "object URL" but every time I try to open it I get:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>7F4BB573F589D927</RequestId>
<HostId>GkYjQGNkrh84HodCaQxfTHKFCDLle82B5d4oa6EyeK1ZJMt/BeZG09eS2CIiR6Ri2Va/IvQIcIE=</HostId>
</Error>
I added bucket policy:
{
"Version": "2012-10-17",
"Id": "Policy1547051060680",
"Statement": [
{
"Sid": "Stmt1547051055882",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket-name06/*"
}
]
}
but it doesn't seem to work. Is there anything else I can do?

You bucket policy is already public so you don't need to modify anything in the policy. You need to set ACL property to 'public-read' when using PutObject API. Also, don't leave bucket policy public assign it a policy to restrict upload.

You will need to give your IAM role, that you are using to access the account, read permissions to the bucket. You can do an attach policy or in json it can look something like this
{
"Sid": "S3Read",
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::bucket-name06",
"arn:aws:s3:::bucket-name06/*"
]
}
Assuming the s3 bucket is on the same account as the IAM role you are trying to access you do not need to enable cross account access. If they are not, you will need to allow on the bucket side for the IAM role to read the bucket.
If you need write access you can do something similar to this
{
"Sid": "S3Write",
"Effect": "Allow",
"Action": [
"s3:AbortMultipartUpload",
"s3:DeleteObject",
"s3:Get*",
"s3:List*",
"s3:Put*",
],
"Resource": [
"arn:aws:s3:::bucket-name06",
"arn:aws:s3:::bucket-name06/*"
]
}

Related

How to restrict all access to the AWS s3 bucket

I want to restrict aws s3 bucket to not get access from anywhere, I want block all access public, private, bucket, folder, file everything of that bucket after that then i want to create an access point of s3 then I want to give permission to an IAM user so that only that IAM user can perform all action but only that IAM user
now I am not sure what exactly I also enable or disable like public access or something
also, i don't know I have to give a policy to the bucket or access point
I want to restrict aws s3 bucket to not get access from anywhere, I want block all access public, private, bucket, folder, file everything of that bucket
Use this policy to restrict all access:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyAll",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*"
}
]
}
then i want to create an access point of s3 then I want to give permission to an IAM user so that only that IAM user can perform all action but only that IAM user
Use this policy to restrict all access except for one IAM user:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyAllExceptRole",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*",
"Condition": {
"StringNotEquals": {
"aws:PrincipalArn": "IAM-ROLE-ARN"
}
}
},
{
"Sid": "AllowRole",
"Effect": "Allow",
"Principal": "IAM-ROLE-ARN",
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*"
}
]
}

Basic idea to access items in S3 bucket from Browser

I use [django-s3direct][1] to upload file to S3 bucket.
Once file is uploaded there comes url appeares here.
https://s3.ap-northeast-1.amazonaws.com/cdk-sample-bk/line-assets/images/e236fc508939466a96df6b6066f418ec/1040
However when accessing from browser, the error comes.
<Error>
<script/>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>025WQBJQ5K2W5Z5W</RequestId>
<HostId>FF3VeIft8zSQ7mRK1a5e4l8jolxHBB40TEh6cPhW0qQtDqT7k3ptgCQt3/nusiehDIXkgvxXkcc=</HostId>
</Error>
Now I can use s3.ap-northeast-1.amazonaws.com url? or do I need to create access point ?
Access permission is public and bloc public access is off
Bucket policy is like this
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::678100228133:role/st-dev-base-stack-CustomS3AutoDeleteObjectsCustomR-MLBJDQF3OWFJ"
},
"Action": [
"s3:GetBucket*",
"s3:List*",
"s3:DeleteObject*"
],
"Resource": [
"arn:aws:s3:::cdk-st-dev-sample-bk",
"arn:aws:s3:::cdk-st-dev-sample-bk/*"
]
}
]
}
Is there any other things I need to check?
As #marcin said you bucket policy only allows the actions for the IAM role arn:aws:iam::678100228133:role/st-dev-base-stack-CustomS3AutoDeleteObjectsCustomR-MLBJDQF3OWFJ. If you want to have all your objects accessible to the public (would not recommend write) you need change your bucket policy as following -
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetBucket*",
"s3:GetObject",
"s3:List*",
"s3:DeleteObject*"
],
"Resource": [
"arn:aws:s3:::cdk-st-dev-sample-bk",
"arn:aws:s3:::cdk-st-dev-sample-bk/*"
]
}
]
}
The above policy makes all of your bucket objects accessible to the public (also allows the public to delete them!!). My recommendation will be using django-storages and presigned urls allow your users to access your bucket objects.

AWS Root User Permission Denied on S3 Bucket policy

I created a new bucket on AWS S3 from the web wizard.
I was logged in as root user
I am attempting to add a Bucket policy as follows
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::<my-bucket-name-is-here>/*"
]
}]
}
I get permission denied in both the web editor and the CLI
Web tool
CLI
An error occurred (AccessDenied) when calling the PutBucketPolicy operation: Access Denied
In the IAM settings, the root user has full access
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*"
}
]
I added
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
I also tried adding
{
"Sid": "ModifyBucketPolicy",
"Action": [
"s3:GetBucketPolicy",
"s3:PutBucketPolicy"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<MY-BUCKET-NAME>*"
},
I still don't have permissions
Thanks to #JohnRotenstein I see that because I accepted the default "Block All Public Access" from AWS I was unable to edit the bucket policy. This makes sense, since the bucket policy can also control access and could thus conflict.
However, the error message is confusing since it makes no mention of the fact that it is the Block public access (bucket settings) that prevented updating. The error message stating access denied / you don't have permissions made me think it was the IAM settings on my user that were preventing me from modifying the resource.

AWS S3: An error occurred (AccessDenied) when calling the GetObject operation: Access Denied

I have an AWS account with read/write permissions as shown below:
I'd like to make it so that an IAM user can download files from an S3 bucket but I'm getting access denied when executing aws s3 sync s3://<bucket_name> . I have tried various things, but not to avail. Some steps that I did:
Created a user called s3-full-access
Executed aws configure in my CLI and entered the generated access key id and secret access key for the above user
Created a bucket policy (shown below) that I'd hoped grants access for my user created in first step.
My bucket has a folder name AffectivaLogs in which files were being added anonymously by various users, and it seems like though the bucket is public, the folder inside it is not and I am not even able to make it public, and it leads to following error.
Following are the public access settings:
Update: I updated the bucket policy as follows, but it doesn't work.
To test the situation, I did the following:
Created an IAM User with no attached policies
Created an Amazon S3 bucket
Turned off S3 block public access settings:
Block new public bucket policies
Block public and cross-account access if bucket has public policies
Added a Bucket Policy granting s3:* access to the contents of the bucket for the IAM User
I then ran aws s3 sync and got Access Denied.
I then modified the policy to also permit access to the bucket itself:
{
"Id": "Policy",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement",
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::my-bucket/*",
"arn:aws:s3:::my-bucket"
],
"Principal": {
"AWS": [
"arn:aws:iam::123456789012:user/stack-user"
]
}
}
]
}
This worked.
Bottom line: Also add permissions to access the bucket, in addition to the contents of the bucket. (I suspect it is because aws s3 sync requires listing of bucket contents, in addition to accessing the objects themselves.)
If you use KMS encryption enabled on bucket you should also add policy that allows you to decrypt data using KMS key.
You can configure the S3 policy with the required principal
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListBucket",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::accountId:user/*
},
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::bucket"
},
{
"Sid": "GetObjects",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::accountId:user/*
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::bucket/*"
}
]
}
Or you can create IAM policy and attached it to the role
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListBucket",
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::bucket"
},
{
"Sid": "GetObject",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::bucket/*"
}
]
}

Undo aws s3 policy to deny all users all actions

I accidently set the s3 bucket policy to deny all actions to a bucket for all users
{
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
Now I cant delete anything in the bucket or even remove the bucket. I can't do anything to the bucket anymore. I can't even remove it with cloudformation.
Is there a way to undo this or somehow remove this bucket?
To test this, I created a bucket and added this Bucket Policy:
{
"Id": "TryThis",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "NoBucket",
"Action": "s3:*",
"Effect": "Deny",
"Resource": "arn:aws:s3:::my-bucket",
"Principal": "*"
},
{
"Sid": "NoObjects",
"Action": "s3:*",
"Effect": "Deny",
"Resource": "arn:aws:s3:::my-bucket/*",
"Principal": "*"
}
]
}
Indeed, I was unable to:
List contents
Upload objects
Edit the Bucket Policy
However, I was able to use the Delete Bucket command in the AWS Management Console.
I then repeated the experiment and logged in using my Root Credentials. I was then able to delete the Bucket Policy and restore all functionality to the bucket. Root credentials have full access to an AWS account.