Skip to content

Instantly share code, notes, and snippets.

@jaskiratr
Created April 16, 2017 03:59
Show Gist options
  • Save jaskiratr/54ec145812ae14799481cb5f0aaa5650 to your computer and use it in GitHub Desktop.
Save jaskiratr/54ec145812ae14799481cb5f0aaa5650 to your computer and use it in GitHub Desktop.
Mapbox Datasets API
var request = require('request-promise')
var AWS = require('aws-sdk')
var fs = require('fs')
var userName = 'YOUR_MAPBOX_USERNAME'
var accessToken = 'YOUR_MAPBOX_TOKEN' // From https://www.mapbox.com/studio/account/tokens/
var credentials = null
/*
Methods:
requestCredentials() : To obtain temporary credentials to upload datasets
createUploadCredentials(): To create credentials for uploading a specific file
checkUploads() : To view any existing uploads
createUpload() : Initiate uploading the dataset
removeUpload() : Remove upload from the queue
*/
requestCredentials('./data/buildings.geojson')
.then(res => {
return createUpload(credentials)
})
.then(res => {
console.log('Done Upload')
return checkUploads()
})
.then(res => {
console.log('Upload Status')
console.log(res)
})
.catch(function(err) {
console.log(err)
})
function checkUploads() {
return new Promise((resolve, reject) => {
var options = {
method: 'GET',
uri: `https://api.mapbox.com/uploads/v1/${userName}?access_token=${accessToken}`
}
request(options)
.then(res => {
resolve(res)
})
.catch(function(err) {
reject(err)
})
})
}
function requestCredentials(filepath) {
var options = {
method: 'GET',
uri: `https://api.mapbox.com/uploads/v1/${userName}/credentials?access_token=${accessToken}`
}
return request(options)
.then(res => {
credentials = JSON.parse(res) // Set credentials globally
console.log(`Got credentials`)
return createUploadCredentials(credentials, filepath)
})
}
// Use aws-sdk to stage the file on Amazon S3
function createUploadCredentials(credentials, filepath) {
return new Promise((resolve, reject) => {
console.log(`Staging File`)
var s3 = new AWS.S3({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken,
region: 'us-east-1'
})
s3.putObject({
Bucket: credentials.bucket,
Key: credentials.key,
Body: fs.createReadStream(filepath)
}, function(err, res) {
err ? reject(err) : resolve(res)
})
})
}
function createUpload(credentials) {
return new Promise((resolve, reject) => {
console.log('Uploading to mapbox')
var queryData = {
'url': credentials.url,
'tileset': [userName, 'mytileset'].join('.'),
'name': 'new-dataset'
}
var options = {
method: 'POST',
uri: `https://api.mapbox.com/uploads/v1/${userName}?access_token=${accessToken}`,
json: true,
body: queryData
}
request(options)
.then(res => {
resolve(res)
})
.catch(function(err) {
reject(err)
})
})
}
function removeUpload(id) {
return new Promise((resolve, reject) => {
var options = {
method: 'DELETE',
uri: `https://api.mapbox.com/uploads/v1/${userName}/${id}?access_token=${accessToken}`
}
request(options)
.then(res => {
resolve(res)
})
.catch(function(err) {
reject(err)
})
})
}
{
"name": "dataset",
"version": "1.0.0",
"description": "",
"main": "mapboxDataset.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Jaskirat",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.9.0",
"bluebird": "^3.4.7",
"mapbox": "^1.0.0-beta5",
"request": "^2.79.0",
"request-promise": "^4.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment