无法使用tileset Uploads API将本地json文件上传到mapbox

时间:2017-03-06 11:42:47

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

我正在按照this Mapbox Uploads API documentation中给出的步骤,通过API将我的本地json文件上传到Mapbox。我为此创建了C#控制台应用程序。

我能够获得Mapbox临时s3凭据,但是当我尝试使用这些临时mapbox凭据将文件上传到s3时,我收到以下错误:

  

您提供的AWS访问密钥ID在我们的记录中不存在。

以下是我的控制台应用代码:

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

1 个答案:

答案 0 :(得分:0)

好的,所以我传递了错误的密钥而没有设置sessionToken。我做了以下更改,现在正在运行:

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