Question

I have an ASP.Net MVC project and I need to upload files to RackSpace CloudFiles Asynchronously or using Queue feature in there api, is there any example for it ? I didn't find it on openstack.net documentation and C# code samples.

Was it helpful?

Solution

At the time of writing there is no .net library offering async file uploads for Rackspace Cloud Files. However, there is a promise, that V2 of openstack.net will offer async support.

You may use HttpClient for now:

public static async Task<bool> UploadFileAsync(string filepath, string container, string filename )
{
    var httpClient = new HttpClient();

    var requestMessage = new HttpRequestMessage(HttpMethod.Post,
        @"https://identity.api.rackspacecloud.com/v2.0/tokens")
    {
        Content = new StringContent(
            @"{ 
                ""auth"": { 
                    ""RAX-KSKEY:apiKeyCredentials"": { 
                    ""username"": ""username"",
                    ""apiKey"": ""apikey""
                }
            }
    }")
    };

    requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    var response = await httpClient.SendAsync(requestMessage);

    var responseContent = await response.Content.ReadAsStringAsync();

    var obj = JObject.Parse(responseContent);
    var tokenId = obj.SelectToken("access.token.id").ToObject<string>();

    var endpointPublicURL = obj.SelectToken("access.serviceCatalog[?(@.name == 'cloudFiles')].endpoints[0].publicURL");


    var fileBytes = File.ReadAllBytes(filepath);
    using (var httpContent = new ByteArrayContent(fileBytes))
    {
        httpContent.Headers.Add("X-Auth-Token", tokenId);
        httpContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
        httpContent.Headers.ContentLength = fileBytes.LongLength;

        var result = await httpClient.PutAsync(endpointPublicURL + "/" + container + "/" filename, httpContent);
    }
    return true;
}

OTHER TIPS

Mohamed --

While not asynchronous, I do have an MVC 4 example that shows how to display a Progress Bar during an upload.

It was committed just this morning. You can locate it here:

https://github.com/DonSchenck/ProgressBarMVC4

Let me know if this helps, or if you need more.

All The Best,

-- Don Schenck, OpenStack.NET Developer Advocate, Rackspace

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top