Skip to content

Instantly share code, notes, and snippets.

@vacas
Last active September 18, 2020 00:16
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 vacas/987a255e1c1cfea4ad54df677a9e3256 to your computer and use it in GitHub Desktop.
Save vacas/987a255e1c1cfea4ad54df677a9e3256 to your computer and use it in GitHub Desktop.
A simplified script to upload mapbox tiles using their API, mainly for various files - https://docs.mapbox.com/api/maps/
# update script to set variables or use parameters
username=<username here>
# remember to add token scope uploads:write
accessToken=<secret access token here>
# you can use "$1" for cli paramenter or however you please to set the path to file
pathToFile=</path/to/file here>
# to randomize id; change amount of characters after "head -c". This is mainly created for bash supported OS
export LC_CTYPE=C
randomId=$(head /dev/urandom | tr -dc a-z0-9 | head -c 6 ; echo '')
echo "randomId=${randomId}"
# to get credentials
credentials=$(curl -X POST "https://api.mapbox.com/uploads/v1/${username}/credentials?access_token=${accessToken}")
echo ${credentials}
# separate each variable; requires "brew install jq" if using mac - needs updating if using another OS
bucket=$(echo ${credentials} | jq '.bucket' | tr -d \")
echo "bucket=${bucket}"
accessKey=$(echo ${credentials} | jq '.accessKeyId' | tr -d \")
echo "accessKey=${accessKey}"
mbKey=$(echo ${credentials} | jq '.key' | tr -d \")
echo "mbKey=${mbKey}"
secretAccessKey=$(echo ${credentials} | jq '.secretAccessKey' | tr -d \")
echo "secretAccessKey=${secretAccessKey}"
sessionToken=$(echo ${credentials} | jq '.sessionToken' | tr -d \")
echo "sessionToken=${sessionToken}"
# setting AWS vars
export AWS_SESSION_TOKEN=${sessionToken}
export AWS_SECRET_ACCESS_KEY=${secretAccessKey}
export AWS_ACCESS_KEY_ID=${accessKey}
# staging file
aws s3 cp ${pathToFile} s3://${bucket}/${mbKey} --region us-east-1
# uploading file
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"url": "http://'${bucket}'.s3.amazonaws.com/'${mbKey}'",
"tileset": "'${username}'.'${randomId}'"
'
# name: optional
# private: optional
# for more information: https://docs.mapbox.com/api/maps/#create-an-upload
'
}' 'https://api.mapbox.com/uploads/v1/'${username}'?access_token='${accessToken}''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment