POST to s3 presigned URL results in a 403

0

I am trying to use presigned URLs to upload files to s3. I have the following code in python that generates the presigned URL then used it to POST a file. My s3 bucket doesn't have any special setup but I do have a CORS with POST as the AllowedMethods. When I run the python code I get a 403 and the file is not uploaded. Any suggestions or pointers to follow to resove this would be greatly appreciated.

import boto3
import requests

OBJECT_NAME_TO_UPLOAD = "file_path"


s3_client = boto3.client(
	's3',
	aws_access_key_id = "access_key",
	aws_secret_access_key = "secret_access_key"
)

# generate the presigned URL
response = s3_client.generate_presigned_post(
	Bucket = "bucket-name",
	Key = OBJECT_NAME_TO_UPLOAD,
	ExpiresIn = 60
)

print(response)

# upload to presigned URL
files = { 'file': open(OBJECT_NAME_TO_UPLOAD)}
r = requests.post(response['url'], data=response['fields'], files=files)

print(r.status_code)
asked a year ago345 views
1 Answer
0

https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html

The creator of the URL must have the necessary permissions to upload that object.

The creator in your code is the entity, where you take this access/secret key from

	aws_access_key_id = "access_key",
	aws_secret_access_key = "secret_access_key" 
profile picture
EXPERT
answered a year ago
  • Yes, I verified that the URL creator is able to upload the object.

  • try to add Fields={"Content-Type": "image/jpg"} into s3_client.generate_presigned_post

    choose the needed content type

  • Thanks for the suggestion. Unfortunately accing the Fields has no effect on the result. Still getting a 403.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions