I can't seem to upload a photo to S3. I looked at a lot of resources on the Internet, and I can not find a definite answer to this question. Here is what I still have. I always get the error code: 3 as my failed message.
Client side:
$scope.uploadTopicPhoto = function(imageData) { var image2save = "data:image/jpeg;base64," + imageData; $http({ url: 'http://api.example.io/signS3upload', method: "GET" }).then(function (success) { var options = new FileUploadOptions(); options.fileKey = "file"; options.fileName = success.data.key options.mimeType = "image/jpeg"; options.chunkedMode = false; options.httpMethod = 'PUT'; function win(r) { console.log("Code = " + r.responseCode); } function fail(error) { alert("An error has occurred: Code = " + error.code); } var uri = encodeURI(success.data.signed_request); var ft = new FileTransfer(); ft.upload(image2save, uri, win, fail, options); }); }
Server side:
var s3 = new aws.S3(); var bucketName = 'testimages'; var s3_params = { Bucket: bucketName, Key: uuid.v4() + '.jpg', Expires: 60, ContentEncoding: 'base64', ContentType: 'image/jpeg', ACL: 'public-read' }; s3.getSignedUrl('putObject', s3_params, function(err, data){ if (err) { console.log(err); } else { var return_data = { signed_request: data, key: s3_params.Key }; res.json(return_data); } });
CORS:
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>PUT</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>DELETE</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>Content-*</AllowedHeader> <AllowedHeader>Authorization</AllowedHeader> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
source share