Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var MapboxClient = require('mapbox');
var userName = "spatten";
// This is a private, secret access token - this one has been deleted, but gives you an idea to how it looks
var accessToken = "sk.eyJ1Ijoic3BhdHRlbiIsImEiOiJjaXpyaXIyNXgwMGFwMndwZ3R3dWg0ZmE3In0.xnEazVutD5V9PRD1rqBGxg";
const fs = require('fs');
var client = new MapboxClient(accessToken);
// The first step is to connect to MapBox to provision some temporary access credentials to Amazon's S3
client.createUploadCredentials(function(err, credentials) {
console.log(credentials);
// Use aws-sdk to stage the file on Amazon S3
var AWS = require('aws-sdk');
var s3 = new AWS.S3({
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken,
region: 'us-east-1'
});
function uploadComplete(err, upload) {
if(err) {
console.log("An error occured creating upload: " + err);
}
console.log(upload);
}
function putComplete(err, resp) {
if(err) {
return console.log("Error uploading file");
}
console.log("Instructing MapBox to create TilSet...");
// Create Vector TileSet
client.createUpload({
tileset: [userName, tilesetName].join('.'),
url: credentials.url
}, uploadComplete);
}
// Upload data to S3
s3.putObject({
Bucket: credentials.bucket,
Key: credentials.key,
Body: fs.createReadStream(fileName)
}, putComplete);
});
{
"name": "mapbox-upload-poc",
"version": "1.0.0",
"description": "A proof of concept for uploading to Mapbox",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Sebastian Patten",
"license": "MIT",
"dependencies": {
"aws-sdk": "^2.22.0",
"mapbox": "^1.0.0-beta6"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment