Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created September 20, 2018 07:06
Show Gist options
  • Save lastday154/5110c5f743fbe4f1f0508a04a198e041 to your computer and use it in GitHub Desktop.
Save lastday154/5110c5f743fbe4f1f0508a04a198e041 to your computer and use it in GitHub Desktop.
S3 presigned url
var React = require('react');
var Dropzone = require('react-dropzone');
var axios = require('axios');
exports = module.exports = React.createClass({
_onDrop: function (files) {
var file = files[0];
axios.get(ENDPOINT_TO_GET_SIGNED_URL, {
filename: file.name,
filetype: file.type
})
.then(function (result) {
var signedUrl = result.data.signedUrl;
var options = {
headers: {
'Content-Type': file.type
}
};
return axios.put(signedUrl, file, options);
})
.then(function (result) {
console.log(result);
})
.catch(function (err) {
console.log(err);
});
},
render: function () {
return (
<Dropzone onDrop={ this._onDrop } size={ 150 }>
<div>
Drop some files here!
</div>
</Dropzone>
);
}
});
var aws = require('aws-sdk');
aws.config.update({
accessKeyId: AWS_ACCESS_KEY
secretAccessKey: AWS_SECRET_KEY
});
exports = module.exports = {
sign: function(filename, filetype) {
var s3 = new aws.S3();
var params = {
Bucket: SOME_BUCKET,
Key: filename,
Expires: 60,
ContentType: filetype
};
s3.getSignedUrl(‘putObject’, params, function(err, data) {
if (err) {
console.log(err);
return err;
} else {
return data;
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment