Wednesday, March 15, 2017

Unable to use Mapbox “Create an Upload” api to upload data file

Leave a Comment

I am trying to upload my local json file to mapbox using their upload api. I am following below steps:

  1. Retrieve S3 credentials to stage the file
  2. Use an S3 client, like the AWS SDK, to upload a file to S3 using those credentials
  3. Create an upload using the staged file's URL
  4. Retrieve the upload's status as it is processed into a tileset
  5. Once the upload is complete, use the tileset ID like you'd use any other tileset.

I completed steps 1 and 2 but getting following error on steps 3:

The remote server returned an error: (422) Unprocessable Entity.

Below is my code (step 1):

class Program {     static void Main(string[] args)     {         var getCredsUrl= @"https://api.mapbox.com/uploads/v1/{my_mapbox_username}/credentials?access_token={my_mapbox_token}";         var request = (HttpWebRequest)WebRequest.Create(getCredsUrl);         request.AutomaticDecompression = DecompressionMethods.GZip;         using (var response = (HttpWebResponse)request.GetResponse())         using (var stream = response.GetResponseStream())             if (stream != null)                 using (var reader = new StreamReader(stream))                 {                     var res = reader.ReadToEnd();                     var mbS3Credentials = JObject.Parse(res);                     var accessKeyId = (string)mbS3Credentials["accessKeyId"];                     var bucket = (string)mbS3Credentials["bucket"];                     var key = (string)mbS3Credentials["key"];                     var secretAccessKey = (string)mbS3Credentials["secretAccessKey"];                     var sessionToken = (string)mbS3Credentials["sessionToken"];                     var serviceUrl = (string)mbS3Credentials["url"];                      var localFilePath = "c:\\users\\saurabh\\documents\\visual studio 2015\\Projects\\MapboxTileSetUpload\\MapboxTileSetUpload\\data\\localFile.json";                     var amazonS3Uploader = new AmazonS3Uploader(accessKeyId, secretAccessKey, sessionToken, serviceUrl + "localFile.json");                     var suucess = amazonS3Uploader.UploadFile(localFilePath, bucket, key);                     if (suucess)                     {                         string createUploadUrl =                             @"https://api.mapbox.com/uploads/v1/{my_mapbox_username}?access_token={my_mapbox_token}";                         string tileSet = "{my_mapbox_username}.myTileSet";                         string s3BucketFileUrl = serviceUrl + "/localFile.json";                         string name = "example-dataset";                         var createUpload = new MapboxUploader(createUploadUrl, tileSet, s3BucketFileUrl, name);                         createUpload.CreateUpload();                     }                 }     } } 

Upload to Amazon S3 (step 2):

public class AmazonS3Uploader {     private readonly AmazonS3Client _s3Client;      public AmazonS3Uploader(string accessKeyId, string secretAccessKey, string sessionToken, string serviceUrl)     {         var s3Config = new AmazonS3Config         {             ServiceURL = serviceUrl, RegionEndpoint = RegionEndpoint.USEast1,             ForcePathStyle = true,         };         _s3Client = new AmazonS3Client(accessKeyId, secretAccessKey, sessionToken, s3Config);     }       public bool UploadFile(string filePath, string s3BucketName, string key)     {         //save in s3         var s3PutRequest = new PutObjectRequest         {             FilePath = filePath, BucketName = s3BucketName,             Key = key, CannedACL = S3CannedACL.PublicRead         };          s3PutRequest.Headers.Expires = new DateTime(2020, 1, 1);          try         {             PutObjectResponse s3PutResponse = this._s3Client.PutObject(s3PutRequest);             if (s3PutResponse.HttpStatusCode.ToString() == "OK")                 return true;             else                 return false;         }         catch (Exception ex)         {             //handle exceptions             return false;         }     } } 

Create an Mapbxo upload (Step 3: Here I get error):

public class MapboxUploader {     private readonly string _createUploadUrl;     private readonly string _tileSet;     private readonly string _s3Fileurl;     private readonly string _name;     public MapboxUploader(string createUploadUrl, string tileSet, string s3BucketFileUrl, string name)     {         _createUploadUrl = createUploadUrl; _tileSet = tileSet;         _s3Fileurl = s3BucketFileUrl; _name = name;     }      public async Task<bool> HttpClient()     {         using (var client = new WebClient())         {                 client.BaseAddress = new Uri(this._createUploadUrl);             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));             Dictionary<string, string> data = new Dictionary<string, string>() { { "tileset", this._tileSet }, { "url", this._s3Fileurl }, { "name", this._name } };             var postData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");             try             {                 var response = await client.PostAsync(this._createUploadUrl, postData);                 return true;             }             catch (Exception ex)             {                 return false;             }                         }     } } 

My guess is I am not able to upload files to S3 bucket at step 2. Here is the response from step 2: enter image description here

And at step 3, I get an unprocessable error, with error message Could not access url So I guess my S3 bucket file is not accessible.

enter image description here

When I try to enter the uploaded file S3 bucket URL to the browser, I get access denied error:

enter image description here

Mapbox Access Token scopes: enter image description here

What exactly I am missing here.

1 Answers

Answers 1

When you upload the file to the S3 bucket in Step 2, does the file successfully complete the upload?

In Step 3 when you are creating a URL, there is a {username} keyword in your URL string - is that correct, or should that be replaced with your Mapbox username?

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment