Created
November 11, 2017 20:17
-
-
Save jordicenzano/e295e15e6d9327055458b604fba0e6a7 to your computer and use it in GitHub Desktop.
Upload a growing file to S3 (test - NOT working)
This file contains 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
#!/usr/bin/env node | |
const AWS = require('aws-sdk'); | |
const crypto = require('crypto'); | |
const http = require('http'); | |
const stream = require('stream'); | |
const Readable = stream.Readable; | |
//Stream reader | |
class streamFromData extends Readable { | |
constructor(opt) { | |
super(opt); | |
} | |
_read() { | |
//Needs to be implemented in more complex scenarios | |
} | |
} | |
//Helper for date | |
Date.prototype.yyyymmddhhmmss = function() { | |
let yyyy = this.getFullYear(); | |
let mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based | |
let dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate(); | |
let hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours(); | |
let min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes(); | |
let ss = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds(); | |
return "".concat(yyyy).concat(mm).concat(dd).concat(hh).concat(min).concat(ss); | |
}; | |
//Helper to generate random byte array | |
function randomValueHex (len) { | |
return crypto.randomBytes(Math.ceil(len/2)) | |
.toString('hex') // convert to hexadecimal format | |
.slice(0,len); // return required number of characters | |
} | |
//Helper to do HTTP GET | |
function HttpGET(url, callback) { | |
http.get(url, function(res) { | |
// incrementally capture the incoming response body | |
var data = []; | |
res.on('data', function(chunk) { | |
data.push(chunk); | |
}); | |
// do whatever we want with the response once it's done | |
res.on('end', function() { | |
if (res.statusCode < 300){ | |
//OK | |
// pass the relevant data back to the callback | |
callback(null, Buffer.concat(data)); | |
} | |
else { | |
callback(new Error("Downloading: (" + res.statusCode + ") " + res.statusMessage )); | |
} | |
}); | |
}).on('error', function(err) { | |
// handle errors with the request itself | |
callback(err); | |
}); | |
} | |
//Defaults | |
let s3_origin_bucket = "PUT-YOUR-BUCKET-HERE"; | |
let d = new Date(); | |
let s3_origin_path = "test-open-files/jordi-test-stream-" + d.yyyymmddhhmmss() + ".bin"; | |
let get_host = s3_origin_bucket + ".s3.amazonaws.com"; | |
let get_path = s3_origin_path; | |
let get_url = "http://" + get_host + "/" + get_path; | |
console.log("Download URL: " + get_url); | |
// **************** Code starting point *************** | |
//Create source test | |
let stream_test = new streamFromData(); | |
//Create file source | |
let s3 = new AWS.S3({apiVersion: '2006-03-01'}); | |
let s3_upload_params = { | |
Bucket: s3_origin_bucket, | |
Key: s3_origin_path, | |
ACL: "public-read", | |
Body: stream_test | |
}; | |
//Generate random xKB byte array | |
let source_data = randomValueHex(1024); | |
//Prepare the upload (open it) | |
s3.upload(s3_upload_params, function(err, data) { | |
if (err) { | |
console.error("Upload error:" + JSON.stringify(err)); | |
return 1; | |
} | |
console.log("Upload finished: " + JSON.stringify(data)); | |
}); | |
//Open download stream | |
HttpGET(get_url, function (err, data) { | |
if (err) { | |
console.error("Download error:" + err); | |
return 1; | |
} | |
console.log("Download OK. Size: " + data.length.toString()); | |
}); | |
//Fill the source buffer | |
function fillBuffer(index, delay_ms) { | |
if (index >= source_data.length) { | |
stream_test.push(null); | |
} | |
else { | |
stream_test.push(source_data[index]); | |
if (index%10 === 0) | |
console.log("Added to upload index: " + index.toString()); | |
index++; | |
setTimeout(fillBuffer, delay_ms, index, delay_ms); | |
} | |
} | |
fillBuffer(0, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment