Last active
July 10, 2017 20:04
-
-
Save jenningsanderson/bfd8c83424a3fa853f588887c902dc42 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/node | |
/* My perfect Mapbox Upload CLI Script */ | |
var fs = require('fs'); | |
var minimist = require('minimist'); | |
var AWS = require('aws-sdk'); | |
var MapboxClient = require('mapbox'); | |
var USAGE = 'Usage: \n\tnode upload-tiles.js [ OPTIONS ] FILE' + | |
"\nOptions:\n\t--name\t\tThe name of the tileset for Mapbox (mapbox://<username>.<name>)"+ | |
"\n\t--credentials\tThe path to the credentials file (defaults to ./.mapbox-credentials.txt)"; | |
var minimistOpts = { | |
'string' : ['name', 'credentials'] | |
}; | |
var argv = minimist(process.argv.slice(2), minimistOpts); | |
var posParams = argv._; | |
if (posParams.length === 0) { | |
console.error(USAGE); | |
process.exit(1); | |
} | |
options = {} | |
options.filename = argv._[0]; | |
options.credentials = argv.credentials || ".mapbox-credentials.txt" | |
if (argv.name){ | |
options.name = argv.name | |
}else{ | |
var parts = options.filename.split("/") | |
options.name = parts[parts.length-1].substring(0, parts[parts.length-1].indexOf(".mbtiles")); | |
} | |
function startMapboxUpload(creds){ | |
client.createUpload({ | |
tileset: ["jenningsanderson", options.name].join('.'), | |
url: creds.url, | |
name: options.name | |
}, function(err, upload) { | |
if(err){throw err} | |
console.error("Upload Initialized: ") | |
console.error("\tID: " + upload.id) | |
console.error("\tNAME: " + upload.name) | |
console.error("\tCREATED: " + upload.created) | |
console.error("\tPROGRESS: " + upload.progress) | |
console.error("\tERROR: " + upload.error) | |
console.error("===========================") | |
});//end of createUpload | |
} | |
/* | |
Order of Operations: | |
1. Define Client | |
2. Get Credentials | |
3. Upload to S3, get URL back | |
4. If successful, send Mapbox the URL | |
*/ | |
//Runtime starts here. | |
var token = fs.readFileSync(options.credentials) | |
var client = new MapboxClient(token.toString()); | |
client.createUploadCredentials(function(err, credentials) { | |
//Something went wrong. | |
if(err){throw err} | |
console.error("Starting Upload of " + options.filename + " as " + options.name + "...") | |
// Use aws-sdk to stage the file on Amazon S3 | |
s3obj = new AWS.S3({ | |
accessKeyId: credentials.accessKeyId, | |
secretAccessKey: credentials.secretAccessKey, | |
sessionToken: credentials.sessionToken, | |
region: 'us-east-1', | |
}); | |
console.error("Created S3 Credentials from Mapbox Credentials") | |
var body = fs.createReadStream(options.filename) | |
var stat = true | |
var up = s3obj.upload({Body: fs.createReadStream(options.filename), | |
Key: credentials.key, | |
Bucket: credentials.bucket | |
}) | |
up.on('httpUploadProgress', function(progress) { | |
//Just print out progress | |
process.stderr.write("\r"+(progress.loaded/1048576).toFixed(0) + " / " + | |
(progress.total/1048576).toFixed(0) + " mb "); | |
if (progress.loaded == progress.total){ | |
console.error("Upload Complete") | |
} | |
}) | |
up.send(function(err,data){ | |
if(err){ | |
console.error("S3 Upload Failed") | |
throw err | |
}else{ | |
console.error("Now telling Mapbox...") | |
startMapboxUpload(credentials) | |
} | |
})//Submits the job | |
});//end of client.createUploadCredentials |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment