amazon web services - React Native upload to S3 with presigned URL -
been trying no luck upload image s3 react native using pre-signed url. here code:
generate pre-signed url in node:
const s3 = new aws.s3(); const s3params = { bucket: bucket, key: filename, expires: 60, contenttype: 'image/jpeg', acl: 'public-read' }; return s3.getsignedurl('putobject', s3params);
here rn request s3:
var file = { uri: game.picturetosubmituri, type: 'image/jpeg', name: 'image.jpg', }; const xhr = new xmlhttprequest(); var body = new formdata(); body.append('file', file); xhr.open('put', signedrequest); xhr.onreadystatechange = () => { if(xhr.readystate === 4){ if(xhr.status === 200){ alert('posted!'); } else{ alert('could not upload file.'); } } }; xhr.send(body);
game.picturetosubmituri = assets-library://asset/asset.jpg?id=a282a2c5-31c8-489f-9652-7d3bd5a1faa4&ext=jpg
signedrequest = https://my-bucket.s3-us-west-1.amazonaws.com/8bd2d4b9-3206-4bff-944d-e06f872d8be3?awsaccesskeyid=akiaiolhqy4gaxn26foq&content-type=image%2fjpeg&expires=1465671117&signature=bkqip5lgzuyrt2vyl7rqpcxpcps%3d&x-amz-acl=public-read
error message:
<code>signaturedoesnotmatch</code> <message> request signature calculated not match signature provided. check key , signing method. </message>
i can curl , image s3 using generated url, , seem able post requestb.in rn (however can see raw data on requestb.in not 100% sure image there).
based on this, i've narrowed issue down 1) image not correctly uploading period, or 2) somehow way s3 wants request different how coming in.
any muuuuuucchhhh appreciated!
update
can post rn s3 if body text ({'data': 'foo'}). perhaps aws not mutliform data? how can send file in rn???
formdata
create multipart/form-data
request. s3 put
object needs request body file.
you need send file in request body without wrapping formdata
:
function uploadfile(file, signedrequest, url) { const xhr = new xmlhttprequest(); xhr.open('put', signedrequest); xhr.onreadystatechange = function() { if (xhr.readystate === 4) { if(xhr.status === 200) { alert(url); } else { alert('could not upload file.'); } } }; xhr.send(file); };
see https://devcenter.heroku.com/articles/s3-upload-node example in browser. please ensure content-type
header matched signed url request.
Comments
Post a Comment