This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
upload.js
50 lines (40 loc) · 1.5 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var fs = require('fs'),
MapboxClient = require('mapbox'),
AWS = require('aws-sdk');
var districtsFile = process.argv[2],
user = process.env.MAPBOX_USERNAME,
accessToken = process.env.MAPBOX_WRITE_SCOPE_ACCESS_TOKEN;
var tileset_id = user + ".cd-114-2015"; // max 32 characters (including "-labels" added below), only one period
var tileset_name = "US_Congressional_Districts_114th_2015"; // max 64 characters (including "_Labels" added below) no spaces
var client = new MapboxClient(accessToken);
// here's how to upload a file to Mapbox
function upload_tileset(file, id, name) {
client.createUploadCredentials(function(err, credentials) {
console.log('staging', file, '>', id, '...');
// Use aws-sdk to stage the file on Amazon S3
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(file)
}, function(err, resp) {
if (err) throw err;
// Create a Mapbox upload
client.createUpload({
tileset: id,
url: credentials.url,
name: name
}, function(err, upload) {
if (err) throw err;
console.log(file, id, name, 'uploaded, check mapbox.com/studio for updates.');
});
});
});
}
// do the upload
upload_tileset(districtsFile, tileset_id, tileset_name)