如何使用 RESTSharp 和 .net 4.0 将文件上传到 rackspace?

How can I upload a file to rackspace using RESTSharp and .net 4.0?

这是我目前所拥有的但无法正常工作:

    private void _send1(string file)
    {
        var client = new RestClient("https://identity.api.rackspacecloud.com/v2.0");
        var request = new RestRequest("tokens", Method.POST);
        request.RequestFormat = DataFormat.Json;

        string test = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\"{\"username\":\"";
        test += UserName;
        test += "\",\"apiKey\":\"";
        test += MyToken;
        test += "\"}}}";

        request.AddBody(serText);
        request.AddParameter("application/json", test, ParameterType.RequestBody);

        RestResponse response = (RestResponse)client.Execute(request);

        // Content = "{\"badRequest\":{\"code\":400,\"message\":\"java.lang.String cannot be cast to org.json.simple.JSONObject\"}}"
    }

注意:UserName 和 apiKey 是有效的 RackSpace 凭证:-)

谢谢 提前

尝试 2:(在网上找到的)它给了我一个令牌...现在我要用它做什么?

    private void _send2(string file)
    {
        Dictionary<string, object> dictAuth = new Dictionary<string, object>();
        dictAuth.Add("RAX-KSKEY:apiKeyCredentials", new { username = UserName, apiKey = MyToken });
        var auth = new
        {
            auth = dictAuth
        };

        RestClient client = new RestClient("https://identity.api.rackspacecloud.com");
        RestSharp.RestRequest r = new RestRequest("/v2.0/tokens", Method.POST);
        r.AddHeader("Content-Type", "application/json");
        r.RequestFormat = DataFormat.Json;
        r.AddBody(auth);


        RestResponse response = (RestResponse)client.Execute(r);
        // Content = "{\"access\":{\"token\":{\"id\":\"AACCvxjTOXA\",\"expires\":\"2016-04-09T21:12:10.316Z\",\"tenant\":{\"id\":\"572045\",\"name\...
    }

更进一步: 我创建了一个 class 来解析上面步骤 2 中的 URL、tenantID 和令牌 此数据传递给 PostFile 调用:

    private void PostFile(string url, string tenantID, string token, string file)
    {
        string fName = Path.GetFileName(file);

        RestClient client = new RestClient(url);
        string baseURL = string.Format("v1/{0}/Support/{1}", tenantID, fName);

        RestRequest r = new RestRequest(baseURL, Method.POST);
        r.AddHeader("Content-Type", "text/plain");
        r.AddParameter("X-Auth-Token", token);

        r.AddFile(fName, file);

        RestResponse response = (RestResponse)client.Execute(r);

        if( response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            int x = 0;
        }

    }

这是最终起作用的方法:

        bool bRetval = false;
        string fName = Path.GetFileName(file);

        RestClient client = new RestClient(url);
        string baseURL = string.Format("/Support/{0}", fName);

        RestRequest r = new RestRequest(baseURL, Method.PUT);
        r.AddHeader("Content-Type", "text/plain");
        r.AddHeader("X-Auth-Token", token);

        r.AddFile(fName, file);

        RestResponse response = (RestResponse)client.Execute(r);

请参阅上面的 post 以了解导致此功能的支持功能

   private bool PostFile(string url, string token, string file)
    {
        bool bRetval = false;
        string fName = Path.GetFileName(file);

        RestClient client = new RestClient(url);
        string baseURL = string.Format("/Support/{0}", fName);

        RestRequest r = new RestRequest(baseURL, Method.PUT);
        r.AddHeader("Content-Type", "text/plain");
        r.AddHeader("X-Auth-Token", token);

        r.AddFile(fName, file);

        RestResponse response = (RestResponse)client.Execute(r);

        if ( response.StatusCode == System.Net.HttpStatusCode.Created)
        {
            bRetval = true;
        }

        return bRetval;
    }