Created
May 7, 2016 01:10
-
-
Save deanmcpherson/7a8ce23e1893d56c5cbf160142beeb59 to your computer and use it in GitHub Desktop.
Upload image to S3, generates unique name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var FileTransfer = require('@remobile/react-native-file-transfer'); | |
var s3Conf = { | |
key: 'KEY', | |
bucket: 'BUCKET_NAME', | |
aws_url: 'https://BUCKET_NAME.REGION.amazonaws.com' | |
} | |
var guid = (function() { | |
function s4() { | |
return Math.floor((1 + Math.random()) * 0x10000) | |
.toString(16) | |
.substring(1); | |
} | |
return function() { | |
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + | |
s4() + '-' + s4() + s4() + s4(); | |
}; | |
})(); | |
var s3Upload = function(uri) { | |
return new Promise((resolve, reject) => { | |
let fileTransfer = new FileTransfer(); | |
var name = guid() + '.jpg'; | |
var opts = { | |
fileName: name, | |
headers: { | |
'Content-Length': 10000000 | |
}, | |
params: { | |
AWSAccessKeyId: s3Conf.key, | |
filename: name, | |
acl: 'public-read', | |
key: name, | |
policy: 'SIGNED POLICY', | |
signature: 'SIGNATURE', | |
'Content-Type': 'image/jpeg' | |
} | |
}; | |
fileTransfer.upload( | |
uri, | |
encodeURI(s3Conf.aws_url), | |
() => resolve(s3Conf.aws_url + '/' + name), | |
err => reject(err), opts); | |
}) | |
} | |
module.exports = s3Upload; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment