【问题标题】:Unable to use Mapbox "Create an Upload" api to upload data file无法使用 Mapbox“创建上传”api 上传数据文件
【发布时间】:2017-07-26 09:47:55
【问题描述】:

我正在尝试使用他们的upload api 将我的本地 json 文件上传到 mapbox。我正在执行以下步骤:

  1. 检索 S3 凭据以暂存文件
  2. 使用 S3 客户端(如 AWS 开发工具包)通过这些凭证将文件上传到 S3
  3. 使用暂存文件的 URL 创建上传文件
  4. 在上传文件被处理成瓦片集时检索上传的状态
  5. 上传完成后,像使用任何其他图块集一样使用图块集 ID。

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

远程服务器返回错误:(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 步的响应:

在第 3 步,我收到一个无法处理的错误,并显示错误消息 Could not access url 所以我猜我的 S3 存储桶文件无法访问。

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

Mapbox 访问令牌范围:

我在这里缺少什么。

【问题讨论】:

    标签: c# amazon-web-services amazon-s3 mapbox


    【解决方案1】:

    您的错误出现在这一行的第 1 步中:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多