Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created February 17, 2017 21:01
Show Gist options
  • Save mooyoul/3d4d0e9740cb0195b0483e1be5225c52 to your computer and use it in GitHub Desktop.
Save mooyoul/3d4d0e9740cb0195b0483e1be5225c52 to your computer and use it in GitHub Desktop.
store-buffer.js
'use strict';
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 = (url, key) => {
return new Promise((resolve, reject) => {
request({
method: 'GET',
url: url,
encoding: null // returns body as Buffer
}, (e, res, body) => {
if (e) { return reject(e); } // something went wrong during fetch
if (res.statusCode.toString().slice(0, 1) !== '2') { // server respond with unexpected status code
return reject(new Error(`Unexpected status code ${res.statusCode}`));
}
s3.upload({
Bucket: process.env.S3_BUCKET,
Key: key,
ContentType: res.headers['content-type'] || 'application/octet-stream',
Body: body
}, (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