无法使用Mapbox的“创建上传" api上传数据文件 [英] Unable to use Mapbox "Create an Upload" api to upload data file

查看:138
本文介绍了无法使用Mapbox的“创建上传" api上传数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其上传api将我的本地json文件上传到mapbox .我正在按照以下步骤操作:

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

  1. 获取S3凭据以暂存文件
  2. 使用AWS开发工具包之类的S3客户端,使用这些凭据将文件上传到S3
  3. 使用暂存文件的URL创建上传
  4. 在将上载处理为图块集时检索上载状态
  5. 上传完成后,请像使用其他任何图块一样使用图块ID.

我完成了第1步和第2步,但是在第3步出现了以下错误:

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

远程服务器返回错误:(422)无法处理的实体.

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

下面是我的代码(步骤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();
                    }
                }
    }
}

上传到Amazon S3(步骤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;
        }
    }
}

创建Mapbxo上传文件(第3步:出现错误):

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;
            }                
        }
    }
}

我的猜测是我无法在步骤2将文件上传到S3存储桶.这是步骤2的响应:

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

在第3步中,我收到一个无法处理的错误,错误消息为无法访问url ,所以我猜我的S3存储桶文件不可访问.

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.

当我尝试向浏览器输入上传的文件S3存储桶URL时,出现访问被拒绝错误:

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

Mapbox访问令牌范围:

Mapbox Access Token scopes:

我在这里到底想念什么.

What exactly I am missing here.

推荐答案

您的错误在此行的step1中:

your error is in the step1 at this line :

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

文件名"/localFile.json"消失了,因为aws已经为您提供了文件所在的完整路径

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

这篇关于无法使用Mapbox的“创建上传" api上传数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆