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

≯℡__Kan透↙ 提交于 2019-12-10 17:35:31

问题


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:

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.

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

Mapbox Access Token scopes:

What exactly I am missing here.


回答1:


your error is in the step1 at this line :

string s3BucketFileUrl = serviceUrl + "/localFile.json";

the filename "/localFile.json" does not go, because aws already gives you the full path where the file will be



来源:https://stackoverflow.com/questions/42627072/unable-to-use-mapbox-create-an-upload-api-to-upload-data-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!