Created
February 19, 2017 20:49
-
-
Save mooyoul/efe21b18fb513639abe9af5c7c0f5eb7 to your computer and use it in GitHub Desktop.
store-with-log.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 debug = require('debug'); | |
const request = require('request'); | |
const AWS = require('aws-sdk'); | |
const log = debug('store'); | |
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 = (url, key) => { | |
return new Promise((resolve, reject) => { | |
log('fetching %s', url); | |
request({ | |
method: 'GET', | |
url: url, | |
encoding: null // returns body as Buffer | |
}).on('error', (e) => { | |
// something went wrong during fetch avatar. | |
return reject(e); | |
}).on('response', (res) => { | |
log('got response'); | |
log('stream state:', res._readableState); | |
if (res.statusCode.toString().slice(0, 1) !== '2') { // server respond with unexpected status code | |
return reject(new Error(`Unexpected status code ${res.statusCode}`)); | |
} | |
setTimeout(() => log('stream state after 1s:', res._readableState), 1000); | |
}); | |
}); | |
}; | |
module.exports = exports = store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment