我正在尝试使用他们的upload api将我的本地json文件上传到mapbox.我遵循以下步骤:
>检索S3凭据以暂存文件
>使用S3客户端(如AWS SDK)使用这些凭据将文件上载到S3
>使用暂存文件的URL创建上传
>在上传处理到tileset时检索上载的状态
>上传完成后,使用tileset ID,就像使用任何其他tileset一样.
我完成了第1步和第2步,但在第3步中遇到以下错误:
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的响应:
在第3步,我得到一个无法处理的错误,错误消息无法访问url因此我猜我的S3存储桶文件无法访问.
当我尝试将上传的文件S3存储桶URL输入浏览器时,我得到访问被拒绝错误:
我到底错过了什么.
解决方法:
您的错误位于此行的step1中:
string s3BucketFileUrl = serviceUrl + "/localFile.json";
文件名“/localFile.json”没有去,因为aws已经为你提供了文件所在的完整路径