Closed dahlinPL closed 1 year ago
Hi @dahlinPL thanks for reaching out. I tried to reproduce the issue but was not able to. I could generate a working presigned URL for an object in an eu-central-1
bucket using the example code.
A region is excluded from the example as you mentioned but there are a few ways you could configure a region: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html.
Can you check that you have the necessary IAM policies in place to access the object? Here is more info on this topic from the S3 User Guide: https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-presigned-url.html
Dear @tim-finnigan ,
thanks for quick answers. I double checked my deployment details in kubernetes and reason was wrong IAM role attached. I was fooled by no error returned, and assumed that if I received presigned URL, used credentials (which was instance role in that case) has proper permissions.
Thanks for your help, and sorry for taking your time! Have a nice day, Marcin
Hi @dahlinPL ,
I am facing the same issue you described. Could you tell me what IAM role you needed to add and what permissions the role had that fixed the issue?
Thanks, Chris
Hi @dahlinPL ,
I am facing the same issue you described. Could you tell me what IAM role you needed to add and what permissions the role had that fixed the issue?
Thanks, Chris
Hi @gatto-c ,
I've attached custom IAM role with custom S3 bucket permissions. Policy basically should looks like this (in my case there are also other permissions but I guess they are not needed in that case):
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject" ],
"Resource": "arn:aws:s3:::your_bucket/*"
}
]
}
and mine IAM role has also trust relationship set for security reasons:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::account_id:oidc-provider/oidc.region.amazonaws.com/id/someid"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.region.amazonaws.com/id/someid:aud": "sts.amazonaws.com",
"oidc.eks.region.amazonaws.com/id/someid:sub": "system:serviceaccount:somerolename"
}
}
}
]
}
Thanks @dahlinPL This worked, much appreciated!
You also have to provide GetObject access to IAM tole
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject" ], "Resource": "arn:aws:s3:::/" } ] }
Describe the bug
When using example from https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-presigned-urls.html to generate presigned url for file download from S3, it always ends in
AccessDenied
.Expected Behavior
To generate working presigned URL.
Current Behavior
Trying to reach file using generated presigned URL, results in
AccessDenied
error returned by AWS.Reproduction Steps
Out of the box example code mentioned about will fail due to missing AWS region.
Not adding
region
ends inAuthorizationQueryParametersError
with messageError parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting '{my_region_name}'
Below sample code with region added:
URL generated with this code is not working. My observation is that constructed URL is different that URL constructed while generating presigned URL from AWS console (using this link I'm able to download file).
URL generated using AWS console:
https://{bucket_name}.s3.{region}.amazonaws.com/{object_key}
URL generated by boto3:
https://{bucket_name}.s3.amazonaws.com/{object_key}
As you can seeregion
is missing. Trying to reach object ends inAccessDenied
.adding
endpoint_url
ashttps://{bucket_name}.s3.{region}.amazonaws.com
into client config as results in URL:https://{bucket_name}.s3.{region}.amazonaws.com/{bucket_name}/{object_key}
andAccessDenied
adding
endpoint_url
ashttps://{bucket_name}.s3.amazonaws.com
into client config as results in URL:https://s3.{region}.amazonaws.com/{bucket_name}/{object_key}
andAccessDenied
combining above options with
s3.addressing_style
and/ors3.inject_host_prefix
results inAccessDenied
adding
'ResponseContentDisposition': 'inline'
togenerate_presigned_url
Params, as this parameter exists in working URL generated from AWS console change nothing.Tried with
eu-central-1
andap-northeast-2
regions. Results are the same.Possible Solution
It seems that without adding endpoint_url, endpoint provider using wrong input to generate endpoint:
As you can see, for some reason
Region
is set toaws-global
and it results in URLhttps://bucket_name.s3.amazonaws.com
. Later it results in usingus-east-1
region for signing instead of required local region.When I add
endpoint_url
ashttps://{bucket_name}.s3.{region}.amazonaws.com/
it looks likeregion
is properly passed into args:however as you can see, constructed URL is bad, since it include bucket name as prefix and postfix in URL.
Additional Information/Context
No response
SDK version used
1.26.133
Environment details (OS name and version, etc.)
Django 3.2.18 on docker with python:3.10-slim-bullseye on AWS kubernetes