本文介绍了无法使用tileet Uploads API将本地json文件上传到mapbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在按照此Mapbox上传API文档用于通过API将我的本地json文件上传到Mapbox.我已经为此创建了C#控制台应用程序.
I am following steps given in this Mapbox Uploads API documentation for uploading my local json file to Mapbox through API. I've created C# console app for that.
我能够获取Mapbox临时s3凭据,但是当我尝试使用这些临时mapbox凭据将文件上传到s3时,出现以下错误:
I am able to get Mapbox temporary s3 credentials but when I try to upload file to s3 using those temporary mapbox credentials, I get following error:
以下是我的控制台应用程序代码:
Below is my console app code:
class Program
{
static void Main(string[] args)
{
var getS3AccessDetailsUrl = @"https://api.mapbox.com/uploads/v1/{my_mapbox_username}/credentials?access_token=my_mapbox_access_token";
var res = "";
var request = (HttpWebRequest)WebRequest.Create(getS3AccessDetailsUrl);
request.AutomaticDecompression = DecompressionMethods.GZip;
using (var response = (HttpWebResponse)request.GetResponse())
using (var stream = response.GetResponseStream())
if (stream != null)
using (var reader = new StreamReader(stream))
{
res = reader.ReadToEnd();
// Here I am successfully getting all the temp S3 details of Mapbox.
var mbS3Credentials = JObject.Parse(res);
var accessKeyId = (string)mbS3Credentials["accessKeyId"];
var bucket = (string)mbS3Credentials["bucket"];
var secretAccessKey = (string)mbS3Credentials["secretAccessKey"];
var url = (string)mbS3Credentials["url"];
var amazonS3Uploader = new AmazonS3Uploader(accessKeyId, secretAccessKey, url);
var localFilePath = "c:\\users\\saurabh\\documents\\visual studio 2015\\Projects\\MapboxTileSetUpload\\MapboxTileSetUpload\\data\\geoFile.json";
var newFileName = "testFile";
amazonS3Uploader.UploadFile(localFilePath, bucket, newFileName, false);
}
Console.WriteLine(res);
}
}
public class AmazonS3Uploader
{
private readonly AmazonS3Client _s3Client;
public AmazonS3Uploader(string accessKeyId, string secretAccessKey, string serviceUrl)
{
var s3Config = new AmazonS3Config
{
ServiceURL = serviceUrl,
RegionEndpoint = RegionEndpoint.USEast1,
ForcePathStyle = true,
};
_s3Client = new AmazonS3Client(accessKeyId, secretAccessKey, s3Config);
}
public void UploadFile(string filePath, string s3BucketName, string newFileName, bool deleteLocalFileOnSuccess)
{
//save in s3
var s3PutRequest = new PutObjectRequest
{
FilePath = filePath,
BucketName = s3BucketName,
CannedACL = S3CannedACL.PublicRead
};
//key - new file name
if (!string.IsNullOrWhiteSpace(newFileName))
{
s3PutRequest.Key = newFileName;
}
s3PutRequest.Headers.Expires = new DateTime(2020, 1, 1);
try
{
var s3PutResponse = this._s3Client.PutObject(s3PutRequest);
if (deleteLocalFileOnSuccess)
{
//Delete local file
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
catch (Exception ex)
{
//gets exception here:
//The AWS Access Key Id you provided does not exist in our records.
}
}
}
推荐答案
好,所以我传递了错误的密钥,并且没有设置sessionToken.按照我所做的更改,它现在可以正常工作:
Ok, so I was passing wrong key and was not setting the sessionToken. Following changes I made, and it is working now:
...
var key = (string)mbS3Credentials["key"];
var sessionToken = (string)mbS3Credentials["sessionToken"];
...
var amazonS3Uploader = new AmazonS3Uploader(accessKeyId, secretAccessKey, sessionToken, url);
...
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 void UploadFile(string filePath, string s3BucketName, string key, string newFileName, bool deleteLocalFileOnSuccess)
{
//save in s3
var s3PutRequest = new PutObjectRequest
{
FilePath = filePath,
BucketName = s3BucketName,
Key = key,
CannedACL = S3CannedACL.PublicRead
};
//key - new file name
//if (!string.IsNullOrWhiteSpace(newFileName))
//{
// s3PutRequest.Key = newFileName;
//}
这篇关于无法使用tileet Uploads API将本地json文件上传到mapbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!