Created
February 17, 2017 22:06
-
-
Save mooyoul/550326977415d9707fa6dccba9c0b634 to your computer and use it in GitHub Desktop.
store-file-stream.js
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
'use strict'; | |
const fs = require('fs'); | |
const request = require('request'); | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3({apiVersion: '2006-03-01', region: 'ap-northeast-2'}); | |
// fetch binary from `url` and upload to s3 using `key` as object key | |
const store = (path, key) => { | |
return new Promise((resolve, reject) => { | |
const file = fs.createReadStream(path) | |
.on('error', (e) => { | |
// something went wrong during fetch avatar. | |
return reject(e); | |
}); | |
s3.upload({ | |
Bucket: process.env.S3_BUCKET, | |
Key: key, | |
ContentType: 'application/octet-stream', | |
Body: file // note that `res` is Readable Stream, not Buffer. | |
}, (e, data) => { | |
if (e) { return reject(e); } | |
resolve(data); | |
}); | |
}); | |
}; | |
module.exports = exports = store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment