2017-03-06 10 views
1

APIを使用してローカルjsonファイルをMapboxにアップロードする手順は、this Mapbox Uploads API documentationに記載されています。私はそれのためのC#のコンソールアプリを作成しました。tileset Uploads APIを使用してローカルjsonファイルをマップボックスにアップロードできません

私はMapbox一時的S3の資格情報を取得することができるが、私はそれらの一時mapboxの資格情報を使用してS3にファイルをアップロードしようとすると、私はエラー以下の取得:以下

The AWS Access Key Id you provided does not exist in our records.

は私のコンソールアプリケーションのコードです:

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

答えて

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; 
      //} 
関連する問題