如何使用.net将文件上传到RackSpace云文件?

时间:2013-03-29 21:01:30

标签: asp.net-mvc-3 c#-4.0 .net-4.0 rackspace-cloud rackspace

我想使用RackSpace的CDN(云文件)来存储我的静态内容。问题是我正在使用ASP.NET MVC3,我还没有看到任何当前有关如何做到的工作示例。

我在网上看到了几个例子,但它们都是RackSpace不再支持的版本。您可以在 Note

上确认这一点

http://www.rackspace.com/knowledge_center/article/sample-csharp-application-for-cloud-files

当您在openstack.net下检查RackSpace的gitHub帐户时,他们支持重定向到该项目,您会看到不支持将文件上传到Cloud Files。你可以在Files表中看到:

https://github.com/rackspace/openstack.net/wiki/Feature-Support

如何使用C#以编程方式将文件上传到RackSpace的Cloud Files?

2 个答案:

答案 0 :(得分:4)

你是对的,目前openstack.net项目不支持CloudFiles。该功能定于本月晚些时候(4月中旬)发布。如果您愿意,仍然可以使用csharp-cloudfiles(https://github.com/rackerlabs/csharp-cloudfiles),因为它仍然有效,它只是处于弃用状态,并且不会接收任何更新或错误修复。

但是,这是一个快速而肮脏的控制台应用程序,使用内置的HttpWebRequest / Response对象,可以帮助您在openstack.net v1.1发布之前开始使用。

class Program
{
    const string filePath = @"Path\FileName";
    const string myToken = "myToken";
    const string url = "Url/Container/FileName";

    static void Main(string[] args)
    {
        //Do();
        DoAsync();
    }

    static void Do()
    {
        using (var fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
        {
            UploadStream(new Uri(url), fs, myToken,
                (response) => Console.WriteLine("Upload Complete.  Status Code: {0} ({1})", response.StatusCode, response.StatusDescription));
        }

        Console.WriteLine("Done!");
        Console.ReadLine();
    }

    static void DoAsync()
    {
        IAsyncResult asyncRes;
        using (var fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
        {
            asyncRes = UploadStreamAsync(new Uri(url), fs, myToken,
                (asyncResult) =>
                {
                    var request = (HttpWebRequest)asyncResult.AsyncState;
                    var response = (HttpWebResponse)request.EndGetResponse(asyncResult);
                    Console.WriteLine("Upload Complete.  Status Code: {0} ({1})", response.StatusCode, response.StatusDescription);
                });
        }

        asyncRes.AsyncWaitHandle.WaitOne();

        Console.WriteLine("Done!");
        Console.ReadLine();
    }

    static IAsyncResult UploadStreamAsync(Uri uri, Stream contents, string token, AsyncCallback resultCallback, int chunkSize = 8 * 1024)
    {
        var req = (HttpWebRequest)WebRequest.Create(uri);
        req.Headers.Add("X-Auth-Token", token);
        req.Method = "PUT";

        req.AllowWriteStreamBuffering = false;
        if (req.ContentLength == -1L)
            req.SendChunked = true;

        using (Stream stream = req.GetRequestStream())
        {
            var buffer = new byte[chunkSize];
            int count;
            while ((count = contents.Read(buffer, 0, chunkSize)) > 0)
            {
                stream.Write(buffer, 0, count);
            }
        }

        return req.BeginGetResponse(resultCallback, req);
    }

    static void UploadStream(Uri uri, Stream contents, string token, Action<HttpWebResponse> resultCallback, int chunkSize = 8 * 1024)
    {
        var req = (HttpWebRequest)WebRequest.Create(uri);
        req.Headers.Add("X-Auth-Token", token);
        req.Method = "PUT";

        req.AllowWriteStreamBuffering = false;
        if (req.ContentLength == -1L)
            req.SendChunked = true;

        using (Stream stream = req.GetRequestStream())
        {
            var buffer = new byte[chunkSize];
            int count;
            while ((count = contents.Read(buffer, 0, chunkSize)) > 0)
            {
                stream.Write(buffer, 0, count);
            }
        }

        using (var resp = req.GetResponse() as HttpWebResponse)
        {
            resultCallback(resp);
        }
    }
}

在上面的代码中,我提供了Async和Async两种方法,因此您不需要两者,只需使用最适合您的方法。

如果安装了Http Client NuGet包(http://nuget.org/packages/Microsoft.Net.Http/2.0.20710.0),也可以使用HttpClient库。这是一个示例:

static void UploadFileToRSCloud(string path, string filename)
{
    HttpClient client = new HttpClient();
    MemoryStream ms = new MemoryStream();

    client.DefaultRequestHeaders.TransferEncodingChunked = true;

    var filePath = 
    // Copy the data to the Resource Part 
    using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
    {
        CopyStream(fileStream, ms);
    }// end:using(fileStream) - Close and dispose fileStream. 

    ms.Position = 0;
    StreamContent sc = new StreamContent(ms);

    client.DefaultRequestHeaders.Add("X-Auth-Token", "TOKEN HERE");

    var result = client.PutAsync( "UrlBase/Container/ChipperJones.jpg", sc);

    result.Wait();
}

private static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}

希望有所帮助!

答案 1 :(得分:2)

截至2014年并回复Marc M,这是应该如何做的:

首先安装openstack.net包

https://www.nuget.org/packages/openstack.net/

PM> Install-Package openstack.net

要上传的代码:

public static string Upload(Stream stream, string fileName)
{
    CloudFilesProvider cloudFilesProvider = getProvider();
    IEnumerable<string> containerObjectList = 
        cloudFilesProvider.ListObjects(CONTAINER_NAME, region: REGION_DFW)
        .Select(o => o.Name);
    string extension = Path.GetExtension(fileName);
    string name = Path.GetFileNameWithoutExtension(fileName);

    stream.Position = 0;

    name = name.GenerateSlug(); //my method
    fileName = name + extension;

    while (containerObjectList.Contains(fileName))
        fileName = name + Guid.NewGuid().ToString().Split('-')[0] + extension;

    cloudFilesProvider
        .CreateObject(CONTAINER_NAME, stream, fileName, region: REGION_DFW);

    return Constants.Rackspace.CloudFiles.PUBLIC_CONTAINER + fileName;
}

private static CloudFilesProvider getProvider()
{
    CloudIdentity cloudIdentity = 
        new CloudIdentity() { APIKey = API_KEY, Username = USERNAME };
    return new CloudFilesProvider(cloudIdentity);
}

Usings:

using net.openstack.Core.Domain;
using net.openstack.Providers.Rackspace;

请注意,某些部分是我的技术要求,例如使用Guid,Region等生成slu ..