amazon s3 - nativescript-background-http file uploading to s3 -
based on nativescript: camera takepicture , upload nativescript-background-http
i trying write class take care of file uploading s3 me.
my upload function looks this:
upload() { const format = enumsmodule.imageformat.png cameramodule.takepicture().then(img => { let savepath = fsmodule.knownfolders.documents().path; console.log('save path', savepath); let filename = "img_" + new date().gettime() + "." + format; console.log('filename', filename); let filepath = fsmodule.path.join(savepath, filename); console.log('filepath', filepath); if (img.savetofile(filepath, format)) { let s3upload = new s3upload(format, filename, filepath, this.progresscallback); s3upload.getsignedrequest() .then((url) => { alert(url); }) .catch(error => { console.log('error!'); alert(error); }) } }); } progresscallback(e: any) { console.log(e); }
and file uploader looks this:
import {config} "../../../shared/config"; import {signs3response} "./signs3response"; export class s3upload { filetype: string; filename: string; filepath: string; progresscallback: (e) => void; constructor(filetype: string, filename: string, filepath: string, progresscallback: (e) => void) { this.filetype = filetype; this.filename = filename; this.filepath = filepath; this.progresscallback = progresscallback; } getsignedrequest() { var xhr = new xmlhttprequest(); return new promise<string>((resolve, reject) => { let urlstring = config.apiurl + "profile/signs3?filename=" + this.filename + "&filetype=" + this.filetype; console.log(urlstring); xhr.open("get", urlstring); xhr.onload = () => { resolve(xhr.response); } xhr.onerror = () => { reject(xhr.response) } xhr.send(); }) .then(() => { console.log(json.parse(xhr.responsetext)); let response: signs3response = <signs3response>json.parse(xhr.responsetext); return this.uploadfile(response.signedrequest, response.url); }) } uploadfile(signedrequest, url) { return new promise<string>((resolve, reject) => { return new promise<string>((resolve, reject) => { var xhr = new xmlhttprequest(); xhr.open("put", signedrequest); xhr.setrequestheader('x-amz-acl', 'public-read'); xhr.onload = () => { console.log("onload, outside", json.stringify(xhr)); if (xhr.status === 200) { console.log('uploaded!'); } }; xhr.onprogress = () => { console.log("progress!"); } xhr.onerror = () => { alert("could not upload file."); }; xhr.send(fs.file.frompath(this.filepath)); }); } }); }
getting signed request working fine when send off amazon uploaded back:
{ "unsent": 0, "opened": 1, "headers_received": 2, "loading": 3, "done": 4, "_responsetype": "", "_listeners": {}, "_readystate": 4, "_options": { "url": "https://mmprofilesimages.s3.amazonaws.com/1464833363226.png?awsaccesskeyid=akiajckl57pamzb54laq&content-type=png&expires=1464833423&signature=vocsvchke%2bdfh9ptxmpluktq2cs%3d&x-amz-acl=public-read", "method": "put", "headers": { "x-amz-acl": "public-read" } }, "_errorflag": false, "_response": {}, "_headers": { "date": "thu, 02 jun 2016 02:09:22 gmt", "server": "amazons3", "x-amz-id-2": "0sqdoho2g0/mricx61fjedxwzrn3igsjrcnv86lo2oydss87cct/xwz0pwdqomr3tyzu3g44fca=", "content-type": "application/xml", "transfer-encoding": "identity", "x-amz-request-id": "44733a49b05581df" }, "_status": 403 }
i know works because doing on in typescript transcompiled blog, real difference that, getting file part of form , here using fs.file.frompath().
i tried make go server working, in s3 logs, have line: a01fa7fe68cb4649bd0d6bc76055584010ef30abc23d1b8968ae1494dfde1dc8 benaychhio [02/jun/2016:01:16:42 +0000] 128.177.172.220 - 2473be6aa6033d7b rest.put.object 1464828438666.png "put /1464828438666.png?awsaccesskeyid=akiajfutn7f7vlar2scq&content-type=png&expires=1464828498&signature={signature here}&x-amz-acl=public-read http/1.1" 403 accessdenied 333 - 4 - "-" "montmatchmobile/1.0 cfnetwork/758.3.15 darwin/15.5.0" -
anyone have advice on this? tried nativescript-background-html not getting that.
try following
tns plugin add nativescript-background-http
i changed couple things plugin adds nice local notification of upload status , name says can upload files when app in background/minimized
var bghttp = require("nativescript-background-http"); var session = bghttp.session("file-upload"); uploadfile(signedrequest, url) { return new promise<string>((resolve, reject) => { var request = { url: signedrequest, method: "put", headers: { "content-type": "application/octet-stream", "file-name": this.filename, "x-amz-acl": 'public-read' }, description: "{ 'uploading': this.filename }" }; var task = session.uploadfile(this.filepath, request); task.on("progress", logevent); task.on("error", logevent); task.on("complete", logevent); function logevent(e) { console.log(e.eventname); } }); });