Skip to content

Instantly share code, notes, and snippets.

@nitrag
Last active February 14, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitrag/a6fd175ba7aae159b9b5aa7337313906 to your computer and use it in GitHub Desktop.
Save nitrag/a6fd175ba7aae159b9b5aa7337313906 to your computer and use it in GitHub Desktop.
Upload file to Mapbox Tileset (node.js)
/**
Uploads a tileset (tested with shapefile.zip) to Mapbox S3 (required),
then triggers an API request to Mapbox to create/update the tileset
Usage:
node uploadToMapBoxTileset.js [path/to/file.zip] [tilesetId] [Tileset Title]
Pro tip: Don't foget to change {username} and {access_token} and/or hardcode the process.argv[] values.
**/
var AWS = require('aws-sdk'),
request = require('request'),
s3 = require('s3');
request("https://api.mapbox.com/uploads/v1/{username}/credentials?access_token={access_token}", function(err, res, body) {
// For dev purposes only
console.log(body);
var keys = JSON.parse(body);
keys.region = "us-east-1";
var client = s3.createClient({
s3Options: keys
});
var params = {
localFile: process.argv[2],
s3Params: {
Bucket: keys.bucket,
Key: keys.key
},
};
var uploader = client.uploadFile(params);
uploader.on('error', function(err) {
console.error("unable to upload:", err.stack);
});
uploader.on('end', function() {
console.log("Finished uploading to Mapbox S3");
//sync tileset
var options = {
uri: "https://api.mapbox.com/uploads/v1/{username}?access_token={access_token}",
method: 'POST',
json: {
tileset: process.argv[3],
url: keys.url,
name: process.argv[4]
}
};
request.post(options, function(err,httpResponse,body){
console.log("Mapbox Tile Sync - "+httpResponse.statusMessage+" ("+httpResponse.statusCode +")");
console.log(body);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment