Search code examples
.net-4.0restsharprackspace

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


Here is what i have so far and it's not working:

    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\"}}"
    }

note: UserName and apiKey are valid RackSpace credentials :-)

Thanks In advance

Try 2: ( found this on the web ) and it gives me a token... now what do I do with it?

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

moving just a bit further: I have create a class that parses out the URL, tenantID and token from Step 2 above This data is passed to the PostFile call:

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

    }

Here is what finally worked:

        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);

Solution

  • See the above post for the supporting functions that lead up to this one

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