question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

boto3 lib of python, s3 image upload using presigned url with content type

See original GitHub issue
s3_con = boto3.client(
    's3',aws_access_key_id='xxxxx', aws_secret_access_key='xxxxx',
    config=Config(signature_version='s3v4'), region_name=AWS_SETUP['S3']['region']
)
url = s3_con.generate_presigned_url(
    'put_object', Params={
        'Bucket':AWS_SETUP['S3']['bucket_name'], 
        'Key':key,'ContentType':'image/jpg'
    },
    ExpiresIn=AWS_SETUP['S3']['expiresInsecs'],
    HttpMethod='PUT'
)
print(url)

Above code is my python code which generating signed url but when I am trying to upload image using signed url I am getting error message from AWS SignatureDoesNotMatch If I will remove ContentType from above code, I am able to upload image but content type is set as application/x-www-form-urlencoded; charset=UTF-8.

I have to set content type as image/jpg or image/png because while accessing, I have to send it to the third party application who needs content type to be set properly.

I am very new to AWS section integration.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
eguvencommented, Jan 22, 2020

@dino-cell https://github.com/boto/boto3/issues/1149#issuecomment-419463129 Try adding endpoint_url to client:

s3_con = boto3.client(
    's3', region_name='us-west-2', endpoint_url='https://s3.us-west-2.amazonaws.com',
)
3reactions
ashishgupta2014commented, Jun 26, 2017
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
// Remember to include jQuery somewhere.

// Remember to include jQuery somewhere.
$(document).ready(function(){

	prisigned_url=[REDACTED];
	$(function() {

	  $('#theForm').on('submit', sendFile);
	});

	function sendFile(e) {
	    e.preventDefault();

	    // get the reference to the actual file in the input
	    var theFormFile = $('#theFile').get()[0].files[0];

	    $.ajax({
	      type: 'PUT',
	      url:prisigned_url, //server will send presigned url to upload image expires in 3600
	      // Content type must much with the parameter you signed your URL with
	      //contentType: 'binary/octet-stream',
	      // this flag is important, if not set, it will try to send data as a form
	      //ContentType: 'image/jpg',
	      processData: false,
	      // the actual file is sent raw
	      data: theFormFile
	    })
	    .success(function(file,response) {
	    	console.log("file=>",file);
	    	console.log("response=>",response);

	      alert('File uploaded');
	    })
	    
	    .error(function() {
	      alert('File NOT uploaded');
	      console.log( arguments);
	    });

	    return false;
  
	  }
});
</script>
</head>
<body>
<form id="theForm" method="POST" enctype="multipart/form-data" >
    <input id="theFile" name="file" type="file"/> 
    <button id="theButton" type="submit">send 1</button>
</form>
</body>
</html>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Presigned URLs — Boto3 Docs 1.26.33 documentation - AWS
A presigned URL is generated by an AWS user who has access to the object. The generated URL is then given to the...
Read more >
AWS S3 Presigned URL Upload Tutorial in Python
Step 1 – Generate the Presigned URL​​ We can do this in python using the boto3 library to request a url from Amazon...
Read more >
python AWS boto3 create presigned url for file upload
I want to use presigned urls, so the django server will sign a url and pass it back to the client, who will...
Read more >
Generating a presigned URL to upload an object
A presigned URL gives you access to the object identified in the URL, provided that the creator of the presigned URL has permissions...
Read more >
boto3 lib of python, s3 image upload using ... - Bountysource
boto3 lib of python, s3 image upload using presigned url with content type.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found