[英]Unable to upload local json file to mapbox using tileset Uploads API
I am following steps given inthis Mapbox Uploads API documentation for uploading my local json file to Mapbox through API.我正在按照此 Mapbox Uploads API 文档中给出的步骤通过 API 将我的本地 json 文件上传到 Mapbox。 I've created C# console app for that.我为此创建了 C# 控制台应用程序。
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:我能够获得 Mapbox 临时 s3 凭据,但是当我尝试使用这些临时 mapbox 凭据将文件上传到 s3 时,出现以下错误:
The AWS Access Key Id you provided does not exist in our records.您提供的 AWS 访问密钥 ID 在我们的记录中不存在。
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.
}
}
}
Ok, so I was passing wrong key and was not setting the sessionToken.好的,所以我传递了错误的密钥并且没有设置 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;
//}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.